/
provider.go
145 lines (125 loc) · 4.37 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/port-labs/terraform-provider-port-labs/internal/cli"
"github.com/port-labs/terraform-provider-port-labs/internal/consts"
"github.com/port-labs/terraform-provider-port-labs/port/action"
"github.com/port-labs/terraform-provider-port-labs/port/action-permissions"
"github.com/port-labs/terraform-provider-port-labs/port/aggregation-properties"
"github.com/port-labs/terraform-provider-port-labs/port/blueprint"
"github.com/port-labs/terraform-provider-port-labs/port/entity"
"github.com/port-labs/terraform-provider-port-labs/port/page"
"github.com/port-labs/terraform-provider-port-labs/port/page-permissions"
"github.com/port-labs/terraform-provider-port-labs/port/scorecard"
"github.com/port-labs/terraform-provider-port-labs/port/team"
"github.com/port-labs/terraform-provider-port-labs/port/webhook"
"github.com/port-labs/terraform-provider-port-labs/version"
"os"
)
var (
_ provider.Provider = &PortLabsProvider{}
)
type PortLabsProvider struct{}
func New() provider.Provider {
return &PortLabsProvider{}
}
func (p *PortLabsProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = consts.ProviderName
}
func (p *PortLabsProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Interact with Port-labs",
Attributes: map[string]schema.Attribute{
"client_id": schema.StringAttribute{
MarkdownDescription: "Client ID for Port-labs",
Optional: true,
},
"secret": schema.StringAttribute{
MarkdownDescription: "Client Secret for Port-labs",
Sensitive: true,
Optional: true,
},
"token": schema.StringAttribute{
MarkdownDescription: "Token for Port-labs",
Sensitive: true,
Optional: true,
},
"base_url": schema.StringAttribute{
Optional: true,
},
},
}
}
func (p *PortLabsProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data *cli.PortProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var baseUrl string
if data.BaseUrl.IsNull() {
baseUrl = os.Getenv("PORT_BASE_URL")
} else {
baseUrl = data.BaseUrl.ValueString()
}
if baseUrl == "" {
baseUrl = consts.DefaultBaseUrl
}
c, err := cli.New(baseUrl, cli.WithHeader("User-Agent", version.ProviderVersion))
if err != nil {
resp.Diagnostics.AddError("Failed to create Port-labs client", err.Error())
return
}
if data.Token.ValueString() != "" {
c.Client.SetAuthToken(data.Token.ValueString())
} else {
var clientID, secret string
if data.ClientId.IsNull() {
clientID = os.Getenv("PORT_CLIENT_ID")
} else {
clientID = data.ClientId.ValueString()
}
if clientID == "" {
resp.Diagnostics.AddError("Unable to find client ID",
"Client ID is required, either set in config or environment variable PORT_CLIENT_ID")
return
}
if data.Secret.IsNull() {
secret = os.Getenv("PORT_CLIENT_SECRET")
} else {
secret = data.Secret.ValueString()
}
if secret == "" {
resp.Diagnostics.AddError("Unable to find client secret",
"Client secret is required, either set in config or environment variable PORT_CLIENT_SECRET")
return
}
_, err = c.Authenticate(ctx, clientID, secret)
if err != nil {
resp.Diagnostics.AddError("Failed to authenticate with Port-labs", err.Error())
return
}
}
resp.ResourceData = c
}
func (p *PortLabsProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
blueprint.NewBlueprintResource,
aggregation_properties.NewAggregationPropertiesResource,
entity.NewEntityResource,
action.NewActionResource,
action_permissions.NewActionPermissionsResource,
webhook.NewWebhookResource,
scorecard.NewScorecardResource,
team.NewTeamResource,
page.NewPageResource,
page_permissions.NewPagePermissionsResource,
}
}
func (p *PortLabsProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{}
}