generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
spot.go
117 lines (96 loc) · 3.24 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
package cloudprovider
import (
"context"
"github.com/harness/harness-go-sdk/harness/cd"
"github.com/harness/harness-go-sdk/harness/cd/cac"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"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 ResourceCloudProviderSpot() *schema.Resource {
providerSchema := map[string]*schema.Schema{
"account_id": {
Description: "The Spot account ID",
Type: schema.TypeString,
Required: true,
},
"token_secret_name": {
Description: "The name of the Harness secret containing the spot account token",
Type: schema.TypeString,
Required: true,
},
}
// usage_scope is not supported because the scope will always be inherited from `token_secret_name`
commonSchema := commonCloudProviderSchema()
delete(commonSchema, "usage_scope")
helpers.MergeSchemas(commonSchema, providerSchema)
return &schema.Resource{
Description: utils.ConfigAsCodeDescription("Resource for creating a Spot cloud provider."),
CreateContext: resourceCloudProviderSpotCreateOrUpdate,
ReadContext: resourceCloudProviderSpotRead,
UpdateContext: resourceCloudProviderSpotCreateOrUpdate,
DeleteContext: resourceCloudProviderDelete,
Schema: providerSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceCloudProviderSpotRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
cp := &cac.SpotInstCloudProvider{}
if err := c.ConfigAsCodeClient.GetCloudProviderById(d.Id(), cp); err != nil {
return diag.FromErr(err)
} else if cp.IsEmpty() {
d.SetId("")
d.MarkNewResource()
return nil
}
return readCloudProviderSpot(c, d, cp)
}
func readCloudProviderSpot(c *cd.ApiClient, d *schema.ResourceData, cp *cac.SpotInstCloudProvider) diag.Diagnostics {
d.SetId(cp.Id)
d.Set("name", cp.Name)
d.Set("account_id", cp.AccountId)
if cp.Token != nil {
d.Set("token_secret_name", cp.Token.Name)
}
return nil
}
func resourceCloudProviderSpotCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
var input *cac.SpotInstCloudProvider
var err error
if d.IsNewResource() {
input = cac.NewEntity(cac.ObjectTypes.SpotInstCloudProvider).(*cac.SpotInstCloudProvider)
} else {
input = &cac.SpotInstCloudProvider{}
if err = c.ConfigAsCodeClient.GetCloudProviderById(d.Id(), input); err != nil {
return diag.FromErr(err)
} else if input.IsEmpty() {
d.SetId("")
d.MarkNewResource()
return nil
}
}
input.Name = d.Get("name").(string)
input.AccountId = d.Get("account_id").(string)
if token := d.Get("token_secret_name").(string); token != "" {
input.Token = &cac.SecretRef{
Name: token,
}
}
cp, err := c.ConfigAsCodeClient.UpsertSpotInstCloudProvider(input)
if err != nil {
return diag.FromErr(err)
}
return readCloudProviderSpot(c, d, cp)
}