-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
220 lines (191 loc) · 6.53 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package atlassian
import (
"context"
"fmt"
"os"
jira "github.com/ctreminiom/go-atlassian/jira/v3"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/openscientia/terraform-provider-atlassian/internal/provider/attribute_validation"
)
var _ provider.Provider = &atlassianProvider{}
type atlassianProvider struct {
jira *jira.Client
configured bool
version string
}
func (p *atlassianProvider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Atlassian",
Attributes: map[string]tfsdk.Attribute{
"url": {
MarkdownDescription: "Atlassian Host URL. Can also be set with the `ATLASSIAN_URL` environment variable.",
Computed: true,
Optional: true,
Type: types.StringType,
Validators: []tfsdk.AttributeValidator{
attribute_validation.UrlWithScheme("https"),
},
},
"username": {
MarkdownDescription: "Atlassian Username. Can also be set with the `ATLASSIAN_USERNAME` environment variable.",
Computed: true,
Optional: true,
Type: types.StringType,
},
"apitoken": {
MarkdownDescription: "Atlassian API Token. Can also be set with the `ATLASSIAN_TOKEN` environment variable.",
Computed: true,
Optional: true,
Sensitive: true,
Type: types.StringType,
},
},
Version: 1,
}, nil
}
type providerData struct {
Url types.String `tfsdk:"url"`
Username types.String `tfsdk:"username"`
ApiToken types.String `tfsdk:"apitoken"`
}
func (p *atlassianProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data providerData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// User must provide a user to the provider
var username string
if data.Username.Unknown {
// Cannot connect to client with an unknown value
resp.Diagnostics.AddWarning(
"Unable to create client.",
"Cannot use unknown value as Username",
)
return
}
if data.Username.Null {
username = os.Getenv("ATLASSIAN_USERNAME")
} else {
username = data.Username.Value
}
if username == "" {
resp.Diagnostics.AddError(
"Unable to find Username value.",
"Username cannot be an empty string.",
)
return
}
// User must provide a password to the provider
var apitoken string
if data.ApiToken.Unknown {
// Cannot connect to client with an unknown value
resp.Diagnostics.AddError(
"Unable to create client.",
"Cannot use unknown value as ApiToken.",
)
return
}
if data.ApiToken.Null {
apitoken = os.Getenv("ATLASSIAN_TOKEN")
} else {
apitoken = data.ApiToken.Value
}
if apitoken == "" {
resp.Diagnostics.AddError(
"Unable to find ApiToken.",
"ApiToken cannot be an empty string.",
)
return
}
// User must specify a host
var url string
if data.Url.Unknown {
// Cannot connect to client with an unknown value
resp.Diagnostics.AddError(
"Unable to create client.",
"Cannot use unknown value as Url.",
)
return
}
if data.Url.Null {
url = os.Getenv("ATLASSIAN_URL")
} else {
url = data.Url.Value
}
if url == "" {
resp.Diagnostics.AddError(
"Unable to find Url.",
"Url cannot be an empty string.",
)
return
}
c, err := jira.New(nil, url)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create client",
"Unable to create Atlassian client:\n\n"+err.Error(),
)
return
}
c.Auth.SetBasicAuth(username, apitoken)
p.jira = c
p.configured = true
}
func (p *atlassianProvider) GetResources(ctx context.Context) (map[string]provider.ResourceType, diag.Diagnostics) {
return map[string]provider.ResourceType{
"atlassian_jira_issue_field_configuration_item": &jiraIssueFieldConfigurationItemResourceType{},
"atlassian_jira_issue_field_configuration": &jiraIssueFieldConfigurationResourceType{},
"atlassian_jira_issue_field_configuration_scheme": &jiraIssueFieldConfigurationSchemeResourceType{},
"atlassian_jira_issue_screen": jiraIssueScreenResourceType{},
"atlassian_jira_issue_type": jiraIssueTypeResourceType{},
"atlassian_jira_issue_type_scheme": jiraIssueTypeSchemeResourceType{},
"atlassian_jira_issue_type_screen_scheme": &jiraIssueTypeScreenSchemeResourceType{},
"atlassian_jira_screen_scheme": &jiraScreenSchemeResourceType{},
}, nil
}
func (p *atlassianProvider) GetDataSources(ctx context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
return map[string]provider.DataSourceType{
"atlassian_jira_issue_field_configuration": &jiraIssueFieldConfigurationDataSourceType{},
"atlassian_jira_issue_screen": jiraIssueScreenDataSourceType{},
"atlassian_jira_issue_type": jiraIssueTypeDataSourceType{},
"atlassian_jira_issue_type_scheme": jiraIssueTypeSchemeDataSourceType{},
"atlassian_jira_issue_type_screen_scheme": &jiraIssueTypeScreenSchemeDataSourceType{},
"atlassian_jira_screen_scheme": &jiraScreenSchemeDataSourceType{},
}, nil
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &atlassianProvider{
version: version,
}
}
}
// convertProviderType is a helper function for NewResource and NewDataSource
// implementations to associate the concrete provider type. Alternatively,
// this helper can be skipped and the provider type can be directly type
// asserted (e.g. provider: in.(*provider)), however using this can prevent
// potential panics.
func convertProviderType(in provider.Provider) (atlassianProvider, diag.Diagnostics) {
var diags diag.Diagnostics
p, ok := in.(*atlassianProvider)
if !ok {
diags.AddError(
"Unexpected Provider Instance Type",
fmt.Sprintf("While creating the data source or resource, an unexpected provider type (%T) was received. This is always a bug in the provider code and should be reported to the provider developers.", p),
)
return atlassianProvider{}, diags
}
if p == nil {
diags.AddError(
"Unexpected Provider Instance Type",
"While creating the data source or resource, an unexpected empty provider instance was received. This is always a bug in the provider code and should be reported to the provider developers.",
)
return atlassianProvider{}, diags
}
return *p, diags
}