This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
resource_service_account.go
157 lines (135 loc) · 5.63 KB
/
resource_service_account.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
// Copyright 2021 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package provider
import (
"context"
"fmt"
iam "github.com/confluentinc/ccloud-sdk-go-v2/iam/v2"
"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"
"log"
"net/http"
)
func serviceAccountResource() *schema.Resource {
return &schema.Resource{
CreateContext: serviceAccountCreate,
ReadContext: serviceAccountRead,
UpdateContext: serviceAccountUpdate,
DeleteContext: serviceAccountDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
paramApiVersion: {
Type: schema.TypeString,
Computed: true,
Description: "API Version defines the schema version of this representation of a Service Account.",
},
paramKind: {
Type: schema.TypeString,
Computed: true,
Description: "Kind defines the object Service Account represents.",
},
paramDisplayName: {
Type: schema.TypeString,
Required: true,
Description: "A human-readable name for the Service Account.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramDescription: {
Type: schema.TypeString,
Optional: true,
Description: "A free-form description of the Service Account.",
},
},
}
}
func serviceAccountUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChangeExcept(paramDescription) {
return diag.Errorf(fmt.Sprintf("only %s field can be updated for a service account", paramDescription))
}
updatedServiceAccount := iam.NewIamV2ServiceAccountUpdate()
updatedDescription := d.Get(paramDescription).(string)
updatedServiceAccount.SetDescription(updatedDescription)
c := meta.(*Client)
_, _, err := c.iamClient.ServiceAccountsIamV2Api.UpdateIamV2ServiceAccount(c.iamApiContext(ctx), d.Id()).IamV2ServiceAccountUpdate(*updatedServiceAccount).Execute()
if err != nil {
return createDiagnosticsWithDetails(err)
}
return serviceAccountRead(ctx, d, meta)
}
func serviceAccountCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
displayName := d.Get(paramDisplayName).(string)
description := d.Get(paramDescription).(string)
serviceAccount := iam.NewIamV2ServiceAccount()
serviceAccount.SetDisplayName(displayName)
serviceAccount.SetDescription(description)
createdServiceAccount, resp, err := executeServiceAccountCreate(c.iamApiContext(ctx), c, serviceAccount)
if err != nil {
log.Printf("[ERROR] service account create failed %v, %v, %s", serviceAccount, resp, err)
return createDiagnosticsWithDetails(err)
}
d.SetId(createdServiceAccount.GetId())
log.Printf("[DEBUG] Created service account %s", createdServiceAccount.GetId())
return serviceAccountRead(ctx, d, meta)
}
func executeServiceAccountCreate(ctx context.Context, c *Client, serviceAccount *iam.IamV2ServiceAccount) (iam.IamV2ServiceAccount, *http.Response, error) {
req := c.iamClient.ServiceAccountsIamV2Api.CreateIamV2ServiceAccount(c.iamApiContext(ctx)).IamV2ServiceAccount(*serviceAccount)
return req.Execute()
}
func serviceAccountDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
req := c.iamClient.ServiceAccountsIamV2Api.DeleteIamV2ServiceAccount(c.iamApiContext(ctx), d.Id())
_, err := req.Execute()
if err != nil {
return diag.Errorf("error deleting service account (%s), err: %s", d.Id(), err)
}
return nil
}
func executeServiceAccountRead(ctx context.Context, c *Client, serviceAccountId string) (iam.IamV2ServiceAccount, *http.Response, error) {
req := c.iamClient.ServiceAccountsIamV2Api.GetIamV2ServiceAccount(c.iamApiContext(ctx), serviceAccountId)
return req.Execute()
}
func serviceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] Service account read for %s", d.Id())
c := meta.(*Client)
serviceAccount, resp, err := executeServiceAccountRead(c.iamApiContext(ctx), c, d.Id())
if err != nil {
log.Printf("[WARN] Service account get failed for id %s, %v, %s", d.Id(), resp, err)
// https://learn.hashicorp.com/tutorials/terraform/provider-setup
isResourceNotFound := HasStatusNotFound(resp)
if isResourceNotFound && !d.IsNewResource() {
log.Printf("[WARN] Service account with id=%s is not found", d.Id())
// If the resource isn't available, Terraform destroys the resource in state.
d.SetId("")
return nil
}
return createDiagnosticsWithDetails(err)
}
if err := d.Set(paramApiVersion, serviceAccount.GetApiVersion()); err != nil {
return createDiagnosticsWithDetails(err)
}
if err := d.Set(paramKind, serviceAccount.GetKind()); err != nil {
return createDiagnosticsWithDetails(err)
}
if err := d.Set(paramDisplayName, serviceAccount.GetDisplayName()); err != nil {
return createDiagnosticsWithDetails(err)
}
if err := d.Set(paramDescription, serviceAccount.GetDescription()); err != nil {
return createDiagnosticsWithDetails(err)
}
return nil
}