-
Notifications
You must be signed in to change notification settings - Fork 56
/
resource_spotinst_multai_deployment.go
178 lines (142 loc) · 5.42 KB
/
resource_spotinst_multai_deployment.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
package spotinst
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/multai"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
"github.com/spotinst/terraform-provider-spotinst/spotinst/multai_deployment"
)
func resourceSpotinstMultaiDeployment() *schema.Resource {
setupMultaiDeploymentResource()
return &schema.Resource{
Create: resourceSpotinstMultaiDeploymentCreate,
Read: resourceSpotinstMultaiDeploymentRead,
Update: resourceSpotinstMultaiDeploymentUpdate,
Delete: resourceSpotinstMultaiDeploymentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: commons.MultaiDeploymentResource.GetSchemaMap(),
}
}
func setupMultaiDeploymentResource() {
fieldsMap := make(map[commons.FieldName]*commons.GenericField)
multai_deployment.Setup(fieldsMap)
commons.MultaiDeploymentResource = commons.NewMultaiDeploymentResource(fieldsMap)
}
func resourceSpotinstMultaiDeploymentCreate(resourceData *schema.ResourceData, meta interface{}) error {
log.Printf(string(commons.ResourceOnCreate),
commons.MultaiDeploymentResource.GetName())
deployment, err := commons.MultaiDeploymentResource.OnCreate(resourceData, meta)
if err != nil {
return err
}
deploymentId, err := createDeployment(deployment, meta.(*Client))
if err != nil {
return err
}
resourceData.SetId(spotinst.StringValue(deploymentId))
log.Printf("===> Deployment created successfully: %s <===", resourceData.Id())
return resourceSpotinstMultaiDeploymentRead(resourceData, meta)
}
func createDeployment(deployment *multai.Deployment, spotinstClient *Client) (*string, error) {
if json, err := commons.ToJson(deployment); err != nil {
return nil, err
} else {
log.Printf("===> Deployment create configuration: %s", json)
}
var resp *multai.CreateDeploymentOutput = nil
err := resource.Retry(time.Minute, func() *resource.RetryError {
input := &multai.CreateDeploymentInput{Deployment: deployment}
r, err := spotinstClient.multai.CreateDeployment(context.Background(), input)
if err != nil {
return resource.NonRetryableError(err)
}
resp = r
return nil
})
if err != nil {
return nil, fmt.Errorf("[ERROR] failed to create deployment: %s", err)
}
return resp.Deployment.ID, nil
}
func resourceSpotinstMultaiDeploymentRead(resourceData *schema.ResourceData, meta interface{}) error {
deploymentId := resourceData.Id()
log.Printf(string(commons.ResourceOnRead),
commons.MultaiDeploymentResource.GetName(), deploymentId)
input := &multai.ReadDeploymentInput{DeploymentID: spotinst.String(deploymentId)}
resp, err := meta.(*Client).multai.ReadDeployment(context.Background(), input)
if err != nil {
return fmt.Errorf("failed to read deployment: %s", err)
}
// If nothing was found, return no state
deployResponse := resp.Deployment
if deployResponse == nil {
resourceData.SetId("")
return nil
}
if err := commons.MultaiDeploymentResource.OnRead(deployResponse, resourceData, meta); err != nil {
return err
}
log.Printf("===> Deployment read successfully: %s <===", deploymentId)
return nil
}
func resourceSpotinstMultaiDeploymentUpdate(resourceData *schema.ResourceData, meta interface{}) error {
deploymentId := resourceData.Id()
log.Printf(string(commons.ResourceOnUpdate),
commons.MultaiDeploymentResource.GetName(), deploymentId)
shouldUpdate, deployment, err := commons.MultaiDeploymentResource.OnUpdate(resourceData, meta)
if err != nil {
return err
}
if shouldUpdate {
deployment.SetId(spotinst.String(deploymentId))
if err := updateDeployment(deployment, resourceData, meta); err != nil {
return err
}
}
log.Printf("===> Deployment updated successfully: %s <===", deploymentId)
return resourceSpotinstMultaiDeploymentRead(resourceData, meta)
}
func updateDeployment(deployment *multai.Deployment, resourceData *schema.ResourceData, meta interface{}) error {
var input = &multai.UpdateDeploymentInput{Deployment: deployment}
deploymentId := resourceData.Id()
if json, err := commons.ToJson(deployment); err != nil {
return err
} else {
log.Printf("===> Deployment update configuration: %s", json)
}
if _, err := meta.(*Client).multai.UpdateDeployment(context.Background(), input); err != nil {
return fmt.Errorf("[ERROR] Failed to update deployment [%v]: %v", deploymentId, err)
}
return nil
}
func resourceSpotinstMultaiDeploymentDelete(resourceData *schema.ResourceData, meta interface{}) error {
deploymentId := resourceData.Id()
log.Printf(string(commons.ResourceOnDelete),
commons.MultaiDeploymentResource.GetName(), deploymentId)
if err := deleteDeployment(resourceData, meta); err != nil {
return err
}
log.Printf("===> Deployment deleted successfully: %s <===", resourceData.Id())
resourceData.SetId("")
return nil
}
func deleteDeployment(resourceData *schema.ResourceData, meta interface{}) error {
deploymentId := resourceData.Id()
input := &multai.DeleteDeploymentInput{DeploymentID: spotinst.String(deploymentId)}
if json, err := commons.ToJson(input); err != nil {
return err
} else {
log.Printf("===> Deployment delete configuration: %s", json)
}
if _, err := meta.(*Client).multai.DeleteDeployment(context.Background(), input); err != nil {
return fmt.Errorf("[ERROR] onDelete() -> Failed to delete deployment: %s", err)
}
return nil
}