generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
tanzu.go
146 lines (122 loc) · 4.19 KB
/
tanzu.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
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 ResourceCloudProviderTanzu() *schema.Resource {
providerSchema := map[string]*schema.Schema{
"endpoint": {
Description: "The url of the Tanzu platform.",
Type: schema.TypeString,
Required: true,
},
"skip_validation": {
Description: "Skip validation of Tanzu configuration.",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"username": {
Description: "The username to use to authenticate to Tanzu.",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"username_secret_name"},
},
"username_secret_name": {
Description: "The name of the Harness secret containing the username to authenticate to Tanzu with.",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"username"},
},
"password_secret_name": {
Description: "The name of the Harness secret containing the password to use to authenticate to Tanzu.",
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 Tanzu cloud provider."),
CreateContext: resourceCloudProviderTanzuCreateOrUpdate,
ReadContext: resourceCloudProviderTanzuRead,
UpdateContext: resourceCloudProviderTanzuCreateOrUpdate,
DeleteContext: resourceCloudProviderDelete,
Schema: providerSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceCloudProviderTanzuRead(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.PcfCloudProvider{}
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 readCloudProviderTanzu(c, d, cp)
}
func readCloudProviderTanzu(c *cd.ApiClient, d *schema.ResourceData, cp *cac.PcfCloudProvider) diag.Diagnostics {
d.SetId(cp.Id)
d.Set("name", cp.Name)
d.Set("endpoint", cp.EndpointUrl)
d.Set("skip_validation", cp.SkipValidation)
d.Set("username", cp.Username)
if cp.UsernameSecretId != nil {
d.Set("username_secret_name", cp.UsernameSecretId.Name)
}
d.Set("password_secret_name", cp.Password.Name)
return nil
}
func resourceCloudProviderTanzuCreateOrUpdate(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.PcfCloudProvider
var err error
if d.IsNewResource() {
input = cac.NewEntity(cac.ObjectTypes.PcfCloudProvider).(*cac.PcfCloudProvider)
} else {
input = &cac.PcfCloudProvider{}
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.EndpointUrl = d.Get("endpoint").(string)
input.SkipValidation = d.Get("skip_validation").(bool)
input.Username = d.Get("username").(string)
if attr := d.Get("username_secret_name").(string); attr != "" {
input.UsernameSecretId = &cac.SecretRef{
Name: attr,
}
}
input.Password = &cac.SecretRef{
Name: d.Get("password_secret_name").(string),
}
cp, err := c.ConfigAsCodeClient.UpsertPcfCloudProvider(input)
if err != nil {
return diag.FromErr(err)
}
return readCloudProviderTanzu(c, d, cp)
}