generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
infra_aws_ami.go
209 lines (183 loc) · 6.35 KB
/
infra_aws_ami.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
204
205
206
207
208
209
package environment
import (
"fmt"
"strings"
"github.com/harness/harness-go-sdk/harness/cd/cac"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func infraDetailsAwsAmi() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"ami_deployment_type": {
Description: fmt.Sprintf("The ami deployment type to use. Valid options are %s", strings.Join(cac.AmiDeploymentTypesSlice, ", ")),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(cac.AmiDeploymentTypesSlice, false),
},
"asg_identifies_workload": {
Description: "Flag to indicate whether the autoscaling group identifies the workload.",
Type: schema.TypeBool,
Optional: true,
},
"autoscaling_group_name": {
Description: "The name of the autoscaling group.",
Type: schema.TypeString,
Optional: true,
},
"classic_loadbalancers": {
Description: "The classic load balancers to use.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"cloud_provider_name": {
Description: "The name of the cloud provider to connect with.",
Type: schema.TypeString,
Required: true,
},
"hostname_convention": {
Description: fmt.Sprintf("The naming convention to use for the hostname. Defaults to %s", DefaultHostnameConvention),
Type: schema.TypeString,
Optional: true,
Default: DefaultHostnameConvention,
},
"region": {
Description: "The region to deploy to.",
Type: schema.TypeString,
Required: true,
},
"spotinst_cloud_provider_name": {
Description: "The name of the SpotInst cloud provider to connect with.",
Type: schema.TypeString,
Optional: true,
},
"spotinst_config_json": {
Description: "The SpotInst configuration to use.",
Type: schema.TypeString,
Optional: true,
},
"stage_classic_loadbalancers": {
Description: "The staging classic load balancers to use.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"stage_target_group_arns": {
Description: "The staging classic load balancers to use.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"target_group_arns": {
Description: "The ARN's of the target groups.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"use_traffic_shift": {
Description: "Flag to enable traffic shifting.",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func expandAwsAmiConfiguration(d []interface{}, infraDef *cac.InfrastructureDefinition) {
if len(d) == 0 {
return
}
config := d[0].(map[string]interface{})
details := &cac.InfrastructureAwsAmi{}
if attr := config["ami_deployment_type"]; attr != "" {
details.AmiDeploymentType = cac.AmiDeploymentType(attr.(string))
}
if attr := config["asg_identifies_workload"]; attr != nil {
details.ASGIdentifiesWorkload = attr.(bool)
}
if attr := config["autoscaling_group_name"]; attr != "" {
details.AutoscalingGroupName = attr.(string)
}
if attr := config["classic_loadbalancers"]; attr != nil {
lbs := []string{}
for _, lb := range attr.(*schema.Set).List() {
lbs = append(lbs, lb.(string))
}
if len(lbs) > 0 {
details.ClassicLoadBalancers = lbs
}
}
if attr := config["cloud_provider_name"]; attr != "" {
details.CloudProviderName = attr.(string)
}
if attr := config["hostname_convention"]; attr != "" {
details.HostNameConvention = attr.(string)
}
if attr := config["region"]; attr != "" {
details.Region = attr.(string)
}
if attr := config["spotinst_cloud_provider_name"]; attr != "" {
details.SpotinstCloudProviderName = attr.(string)
}
if attr := config["spotinst_config_json"]; attr != "" {
details.SpotinstElastiGroupJson = attr.(string)
}
if attr := config["stage_classic_loadbalancers"]; attr != nil {
lbs := []string{}
for _, lb := range attr.(*schema.Set).List() {
lbs = append(lbs, lb.(string))
}
if len(lbs) > 0 {
details.StageClassicLoadBalancers = lbs
}
}
if attr := config["stage_target_group_arns"]; attr != nil {
groupsArns := []string{}
for _, arn := range attr.(*schema.Set).List() {
groupsArns = append(groupsArns, arn.(string))
}
if len(groupsArns) > 0 {
details.StageTargetGroupArns = groupsArns
}
}
if attr := config["target_group_arns"]; attr != nil {
groupsArns := []string{}
for _, arn := range attr.(*schema.Set).List() {
groupsArns = append(groupsArns, arn.(string))
}
if len(groupsArns) > 0 {
details.TargetGroupArns = groupsArns
}
}
if attr := config["use_traffic_shift"]; attr != nil {
details.UseTrafficShift = attr.(bool)
}
infraDef.InfrastructureDetail = details.ToInfrastructureDetail()
}
func flattenAwsAmiConfiguration(d *schema.ResourceData, infraDef *cac.InfrastructureDefinition) []interface{} {
results := []interface{}{}
if len(infraDef.InfrastructureDetail) == 0 {
return results
}
detail := infraDef.InfrastructureDetail[0]
if detail.Type != cac.InfrastructureTypes.AwsAmi {
return results
}
detailConfig := map[string]interface{}{}
infraDetail := detail.ToAwsAmi()
detailConfig["ami_deployment_type"] = infraDetail.AmiDeploymentType.String()
detailConfig["asg_identifies_workload"] = infraDetail.ASGIdentifiesWorkload
detailConfig["autoscaling_group_name"] = infraDetail.AutoscalingGroupName
detailConfig["classic_loadbalancers"] = infraDetail.ClassicLoadBalancers
detailConfig["cloud_provider_name"] = infraDetail.CloudProviderName
detailConfig["hostname_convention"] = infraDetail.HostNameConvention
detailConfig["region"] = infraDetail.Region
detailConfig["spotinst_cloud_provider_name"] = infraDetail.SpotinstCloudProviderName
detailConfig["spotinst_config_json"] = infraDetail.SpotinstElastiGroupJson
detailConfig["stage_classic_loadbalancers"] = infraDetail.StageClassicLoadBalancers
detailConfig["stage_target_group_arns"] = infraDetail.StageTargetGroupArns
detailConfig["target_group_arns"] = infraDetail.TargetGroupArns
detailConfig["use_traffic_shift"] = infraDetail.UseTrafficShift
return append(results, detailConfig)
}