-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_google_cloud_provider_credential.go
95 lines (82 loc) · 2.89 KB
/
resource_google_cloud_provider_credential.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
package provider
import (
"context"
"github.com/PacketFabric/terraform-provider-packetfabric/internal/packetfabric"
"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 resourceCloudProviderCredentialGoogle() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCloudProviderCredentialGoogleCreate,
ReadContext: resourceCloudProviderCredentialRead,
UpdateContext: resourceCloudProviderCredentialGoogleUpdate,
DeleteContext: resourceCloudProviderCredentialDelete,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "Description of the Cloud Provider Credentials.",
},
"google_service_account": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_CREDENTIALS", nil),
Description: "The Google service account JSON you want to save. " +
"Can also be set with the GOOGLE_CREDENTIALS environment variable.",
},
},
}
}
func resourceCloudProviderCredentialGoogleCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
cpc := extractCloudProviderCredentialsGoogle(d)
resp, err := c.CreateCloudProviderCredential(cpc)
if err != nil {
return diag.FromErr(err)
}
d.SetId(resp.CloudProviderCredentialUUID)
return diags
}
func resourceCloudProviderCredentialGoogleUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
cpcID := d.Id()
cpc := packetfabric.CloudProviderCredentialUpdate{}
if d.HasChange("description") {
cpc.Description = d.Get("description").(string)
}
credentials := packetfabric.CloudCredentials{}
if googleServiceAccount, ok := d.GetOk("google_service_account"); ok {
credentials.GoogleServiceAccount = googleServiceAccount.(string)
}
cpc.CloudCredentials = credentials
_, err := c.UpdateCloudProviderCredential(cpc, cpcID)
if err != nil {
return diag.FromErr(err)
}
d.SetId(cpcID)
return diags
}
func extractCloudProviderCredentialsGoogle(d *schema.ResourceData) packetfabric.CloudProviderCredentialCreate {
cpc := packetfabric.CloudProviderCredentialCreate{}
cpc.CloudProvider = "google"
if description, ok := d.GetOk("description"); ok {
cpc.Description = description.(string)
}
cloudCredentials := packetfabric.CloudCredentials{}
if googleServiceAccount, ok := d.GetOk("google_service_account"); ok {
cloudCredentials.GoogleServiceAccount = googleServiceAccount.(string)
}
cpc.CloudCredentials = cloudCredentials
return cpc
}