generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
spot.go
157 lines (133 loc) · 5.02 KB
/
spot.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
package connector
import (
"context"
"fmt"
"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 ResourceConnectorSpot() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Spot connector.",
ReadContext: resourceConnectorSpotRead,
CreateContext: resourceConnectorSpotCreateOrUpdate,
UpdateContext: resourceConnectorSpotCreateOrUpdate,
DeleteContext: resourceConnectorDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"permanent_token": {
Description: "Authenticate to Spot using account id and permanent token.",
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"spot_account_id": {
Description: "Spot account id.",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"permanent_token.0.spot_account_id_ref"},
AtLeastOneOf: []string{"permanent_token.0.spot_account_id", "permanent_token.0.spot_account_id_ref"},
},
"spot_account_id_ref": {
Description: "Reference to the Harness secret containing the spot account id." + secret_ref_text,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"permanent_token.0.spot_account_id"},
AtLeastOneOf: []string{"permanent_token.0.spot_account_id", "permanent_token.0.spot_account_id_ref"},
},
"api_token_ref": {
Description: "Reference to the Harness secret containing the permanent api token." + secret_ref_text,
Type: schema.TypeString,
Required: true,
},
"delegate_selectors": {
Description: "Connect only using delegates with these tags.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"execute_on_delegate": {
Description: "Execute on delegate or not.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceConnectorSpotRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := resourceConnectorReadBase(ctx, d, meta, nextgen.ConnectorTypes.Spot)
if err != nil {
return err
}
if conn == nil {
return nil
}
if err := readConnectorSpot(d, conn); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceConnectorSpotCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := buildConnectorSpot(d)
newConn, err := resourceConnectorCreateOrUpdateBase(ctx, d, meta, conn)
if err != nil {
return err
}
if err := readConnectorSpot(d, newConn); err != nil {
return diag.FromErr(err)
}
return nil
}
func buildConnectorSpot(d *schema.ResourceData) *nextgen.ConnectorInfo {
connector := &nextgen.ConnectorInfo{
Type_: nextgen.ConnectorTypes.Spot,
Spot: &nextgen.SpotConnector{
Credential: &nextgen.SpotCredential{},
},
}
if attr, ok := d.GetOk("permanent_token"); ok {
config := attr.([]interface{})[0].(map[string]interface{})
connector.Spot.Credential.Type_ = nextgen.SpotAuthTypes.PermanentTokenConfig
connector.Spot.Credential.PermanentTokenConfig = &nextgen.SpotPermanentTokenConfigSpec{}
if attr := config["spot_account_id"].(string); attr != "" {
connector.Spot.Credential.PermanentTokenConfig.SpotAccountId = attr
}
if attr := config["spot_account_id_ref"].(string); attr != "" {
connector.Spot.Credential.PermanentTokenConfig.SpotAccountIdRef = attr
}
if attr := config["api_token_ref"].(string); attr != "" {
connector.Spot.Credential.PermanentTokenConfig.ApiTokenRef = attr
}
connector.Spot.ExecuteOnDelegate = config["execute_on_delegate"].(bool)
if attr := config["delegate_selectors"].(*schema.Set).List(); len(attr) > 0 {
connector.Spot.DelegateSelectors = utils.InterfaceSliceToStringSlice(attr)
}
}
return connector
}
func readConnectorSpot(d *schema.ResourceData, connector *nextgen.ConnectorInfo) error {
switch connector.Spot.Credential.Type_ {
case nextgen.SpotAuthTypes.PermanentTokenConfig:
d.Set("permanent_token", []map[string]interface{}{
{
"spot_account_id": connector.Spot.Credential.PermanentTokenConfig.SpotAccountId,
"spot_account_id_ref": connector.Spot.Credential.PermanentTokenConfig.SpotAccountIdRef,
"api_token_ref": connector.Spot.Credential.PermanentTokenConfig.ApiTokenRef,
"execute_on_delegate": connector.Spot.ExecuteOnDelegate,
"delegate_selectors": connector.Spot.DelegateSelectors,
},
})
default:
return fmt.Errorf("unsupported spot credential type: %s", connector.Spot.Credential.Type_)
}
return nil
}