generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
jira.go
131 lines (107 loc) · 3.63 KB
/
jira.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
package connector
import (
"context"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceConnectorJira() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Jira connector.",
ReadContext: resourceConnectorJiraRead,
CreateContext: resourceConnectorJiraCreateOrUpdate,
UpdateContext: resourceConnectorJiraCreateOrUpdate,
DeleteContext: resourceConnectorDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"url": {
Description: "URL of the Jira server.",
Type: schema.TypeString,
Required: true,
},
"username": {
Description: "Username to use for authentication.",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"username_ref"},
ExactlyOneOf: []string{"username", "username_ref"},
},
"username_ref": {
Description: "Reference to a secret containing the username to use for authentication." + secret_ref_text,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"username"},
ExactlyOneOf: []string{"username", "username_ref"},
},
"password_ref": {
Description: "Reference to a secret containing the password to use for authentication." + secret_ref_text,
Type: schema.TypeString,
Required: true,
},
"delegate_selectors": {
Description: "Tags to filter delegates for connection.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceConnectorJiraRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := resourceConnectorReadBase(ctx, d, meta, nextgen.ConnectorTypes.Jira)
if err != nil {
return err
}
if conn == nil {
return nil
}
if err := readConnectorJira(d, conn); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceConnectorJiraCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := buildConnectorJira(d)
newConn, err := resourceConnectorCreateOrUpdateBase(ctx, d, meta, conn)
if err != nil {
return err
}
if err := readConnectorJira(d, newConn); err != nil {
return diag.FromErr(err)
}
return nil
}
func buildConnectorJira(d *schema.ResourceData) *nextgen.ConnectorInfo {
connector := &nextgen.ConnectorInfo{
Type_: nextgen.ConnectorTypes.Jira,
Jira: &nextgen.JiraConnector{},
}
if attr, ok := d.GetOk("url"); ok {
connector.Jira.JiraUrl = attr.(string)
}
if attr, ok := d.GetOk("username"); ok {
connector.Jira.Username = attr.(string)
}
if attr, ok := d.GetOk("username_ref"); ok {
connector.Jira.UsernameRef = attr.(string)
}
if attr, ok := d.GetOk("password_ref"); ok {
connector.Jira.PasswordRef = attr.(string)
}
if attr, ok := d.GetOk("delegate_selectors"); ok {
connector.Jira.DelegateSelectors = utils.InterfaceSliceToStringSlice(attr.(*schema.Set).List())
}
return connector
}
func readConnectorJira(d *schema.ResourceData, connector *nextgen.ConnectorInfo) error {
d.Set("url", connector.Jira.JiraUrl)
d.Set("username", connector.Jira.Username)
d.Set("username_ref", connector.Jira.UsernameRef)
d.Set("password_ref", connector.Jira.PasswordRef)
d.Set("delegate_selectors", connector.Jira.DelegateSelectors)
return nil
}