generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
azure.go
150 lines (127 loc) · 4.46 KB
/
azure.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
package cloudprovider
import (
"context"
"fmt"
"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/service/cd/usagescope"
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceCloudProviderAzure() *schema.Resource {
providerSchema := map[string]*schema.Schema{
"environment_type": {
Description: fmt.Sprintf("The type of environment. Valid options are %s", cac.AzureEnvironmentTypesSlice),
Type: schema.TypeString,
Optional: true,
Default: cac.AzureEnvironmentTypes.AzureGlobal.String(),
ValidateFunc: validation.StringInSlice(cac.AzureEnvironmentTypesSlice, false),
},
"client_id": {
Description: "The client id for the Azure application",
Type: schema.TypeString,
Required: true,
},
"tenant_id": {
Description: "The tenant id for the Azure application",
Type: schema.TypeString,
Required: true,
},
"key": {
Description: "The Name of the Harness secret containing the key for the Azure application",
Type: schema.TypeString,
Required: true,
},
}
// usage_scope is not supported because the scope will always be inherited from the secret defined in `key`
commonSchema := commonCloudProviderSchema()
delete(commonSchema, "usage_scope")
helpers.MergeSchemas(commonSchema, providerSchema)
return &schema.Resource{
Description: utils.ConfigAsCodeDescription("Resource for creating an Azure cloud provider."),
CreateContext: resourceCloudProviderAzureCreateOrUpdate,
ReadContext: resourceCloudProviderAzureRead,
UpdateContext: resourceCloudProviderAzureCreateOrUpdate,
DeleteContext: resourceCloudProviderDelete,
Schema: providerSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceCloudProviderAzureRead(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.AzureCloudProvider{}
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 readCloudProviderAzure(c, d, cp)
}
func resourceCloudProviderAzureCreateOrUpdate(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.AzureCloudProvider
var err error
if d.IsNewResource() {
input = cac.NewEntity(cac.ObjectTypes.AzureCloudProvider).(*cac.AzureCloudProvider)
} else {
input = &cac.AzureCloudProvider{}
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.AzureEnvironmentType = cac.AzureEnvironmentType(d.Get("environment_type").(string))
input.ClientId = d.Get("client_id").(string)
input.TenantId = d.Get("tenant_id").(string)
input.Key = &cac.SecretRef{
Name: d.Get("key").(string),
}
if input.UsageRestrictions == nil {
input.UsageRestrictions = &cac.UsageRestrictions{}
}
scopes := d.Get("usage_scope")
if scopes != nil {
if err := usagescope.ExpandUsageRestrictions(c, scopes.(*schema.Set).List(), input.UsageRestrictions); err != nil {
return diag.FromErr(err)
}
}
cp, err := c.ConfigAsCodeClient.UpsertAzureCloudProvider(input)
if err != nil {
return diag.FromErr(err)
}
return readCloudProviderAzure(c, d, cp)
}
func readCloudProviderAzure(c *cd.ApiClient, d *schema.ResourceData, cp *cac.AzureCloudProvider) diag.Diagnostics {
d.SetId(cp.Id)
d.Set("name", cp.Name)
d.Set("environment_type", cp.AzureEnvironmentType)
d.Set("client_id", cp.ClientId)
d.Set("tenant_id", cp.TenantId)
d.Set("key", cp.Key.Name)
scope, err := usagescope.FlattenUsageRestrictions(c, cp.UsageRestrictions)
if err != nil {
return diag.FromErr(err)
}
if len(scope) > 0 {
d.Set("usage_scope", scope)
}
return nil
}