/
resource_instance_profile.go
203 lines (188 loc) · 6.21 KB
/
resource_instance_profile.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package identity
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/databrickslabs/databricks-terraform/common"
"github.com/databrickslabs/databricks-terraform/internal/util"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// InstanceProfileInfo contains the ARN for aws instance profiles
type InstanceProfileInfo struct {
InstanceProfileArn string `json:"instance_profile_arn,omitempty"`
}
// InstanceProfileList ...
type InstanceProfileList struct {
InstanceProfiles []InstanceProfileInfo `json:"instance_profiles,omitempty"`
}
// NewInstanceProfilesAPI creates InstanceProfilesAPI instance from provider meta
func NewInstanceProfilesAPI(ctx context.Context, m interface{}) InstanceProfilesAPI {
return InstanceProfilesAPI{
client: m.(*common.DatabricksClient),
context: ctx,
}
}
// InstanceProfilesAPI exposes the instance profiles api on the AWS deployment of Databricks
type InstanceProfilesAPI struct {
client *common.DatabricksClient
context context.Context
}
// Create creates an instance profile record on Databricks
func (a InstanceProfilesAPI) Create(instanceProfileARN string) error {
return a.client.Post(a.context, "/instance-profiles/add", map[string]interface{}{
"instance_profile_arn": instanceProfileARN,
"skip_validation": false,
}, nil)
}
// Read returns the ARN back if it exists on the Databricks workspace
func (a InstanceProfilesAPI) Read(instanceProfileARN string) (string, error) {
var response string
instanceProfiles, err := a.List()
if err != nil {
return response, err
}
for _, profile := range instanceProfiles {
if profile.InstanceProfileArn == instanceProfileARN {
response = profile.InstanceProfileArn
return response, nil
}
}
return response, common.APIError{
ErrorCode: "NOT_FOUND",
Message: fmt.Sprintf("Instance profile with name: %s not found in "+
"list of instance profiles in the workspace!", instanceProfileARN),
Resource: "/api/2.0/instance-profiles/list",
StatusCode: http.StatusNotFound,
}
}
// List lists all the instance profiles in the workspace
func (a InstanceProfilesAPI) List() ([]InstanceProfileInfo, error) {
var instanceProfilesArnList InstanceProfileList
err := a.client.Get(a.context, "/instance-profiles/list", nil, &instanceProfilesArnList)
return instanceProfilesArnList.InstanceProfiles, err
}
// Delete deletes the instance profile given an instance profile arn
func (a InstanceProfilesAPI) Delete(instanceProfileARN string) error {
return a.client.Post(a.context, "/instance-profiles/remove", map[string]interface{}{
"instance_profile_arn": instanceProfileARN,
}, nil)
}
// IsRegistered checks if instance profile exists
func (a InstanceProfilesAPI) IsRegistered(arn string) bool {
if _, err := a.Read(arn); err == nil {
return true
}
return false
}
// Synchronized test helper for working with only single instance profile
func (a InstanceProfilesAPI) Synchronized(arn string, testCallback func() bool) {
timeout := 30 * time.Minute
err := resource.RetryContext(a.context, timeout,
func() *resource.RetryError {
list, err := a.List()
if err != nil {
return resource.NonRetryableError(err)
}
currentTest := common.Current.GetOrUnknown(a.context)
for _, ip := range list {
if ip.InstanceProfileArn == arn {
log.Printf("[INFO] %s: Waiting to acquire instance profile", currentTest)
return resource.RetryableError(fmt.Errorf(
"%s is registered, waiting to release", arn))
}
}
cbError := resource.RetryContext(a.context, timeout, func() *resource.RetryError {
if a.IsRegistered(arn) {
log.Printf("[INFO] %s: Waiting to acquire instance profile", currentTest)
return resource.RetryableError(fmt.Errorf("%s: Waiting to acquire", currentTest))
}
if !testCallback() {
log.Printf("[INFO] %s: Callback returned false", currentTest)
return resource.RetryableError(fmt.Errorf("%s: Callback returned false", currentTest))
}
log.Printf("[INFO] %s: Successfully tested instance profile", currentTest)
if _, err = a.Read(arn); err == nil {
log.Printf("[INFO] %s: Didn't release instance profile", currentTest)
}
return nil
})
if cbError != nil {
return resource.RetryableError(cbError)
}
return nil
})
if err != nil {
panic(err)
}
}
// ResourceInstanceProfile manages Instance Profile ARN binding
func ResourceInstanceProfile() *schema.Resource {
return util.CommonResource{
Schema: map[string]*schema.Schema{
"instance_profile_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: ValidInstanceProfile,
},
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
profile, err := NewInstanceProfilesAPI(ctx, c).Read(d.Id())
if err != nil {
return err
}
return d.Set("instance_profile_arn", profile)
},
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
ipa := d.Get("instance_profile_arn").(string)
if err := NewInstanceProfilesAPI(ctx, c).Create(ipa); err != nil {
return err
}
d.SetId(ipa)
return nil
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
return NewInstanceProfilesAPI(ctx, c).Delete(d.Id())
},
}.ToResource()
}
// ValidInstanceProfile validate if it's valid instance profile ARN
func ValidInstanceProfile(v interface{}, c cty.Path) diag.Diagnostics {
s, ok := v.(string)
if !ok {
return diag.Diagnostics{
diag.Diagnostic{
AttributePath: c,
Summary: "Invalid ARN",
Detail: "Not a string",
},
}
}
instanceProfileArn, err := arn.Parse(s)
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
AttributePath: c,
Summary: "Invalid ARN",
Detail: err.Error(),
},
}
}
if !strings.HasPrefix(instanceProfileArn.Resource, "instance-profile") {
return diag.Diagnostics{
diag.Diagnostic{
AttributePath: c,
Summary: "Invalid ARN",
Detail: fmt.Sprintf("Not an instance profile ARN: %s", v),
},
}
}
return nil
}