-
Notifications
You must be signed in to change notification settings - Fork 9k
/
resource_aws_servicecatalog_portfolio.go
225 lines (198 loc) · 6.41 KB
/
resource_aws_servicecatalog_portfolio.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsServiceCatalogPortfolio() *schema.Resource {
return &schema.Resource{
Create: resourceAwsServiceCatalogPortfolioCreate,
Read: resourceAwsServiceCatalogPortfolioRead,
Update: resourceAwsServiceCatalogPortfolioUpdate,
Delete: resourceAwsServiceCatalogPortfolioDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"created_time": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 20),
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateMaxLength(2000),
},
"provider_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 20),
},
"tags": tagsSchema(),
},
}
}
func resourceAwsServiceCatalogPortfolioCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).scconn
input := servicecatalog.CreatePortfolioInput{
AcceptLanguage: aws.String("en"),
}
name := d.Get("name").(string)
input.DisplayName = &name
now := time.Now()
input.IdempotencyToken = aws.String(fmt.Sprintf("%d", now.UnixNano()))
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("provider_name"); ok {
input.ProviderName = aws.String(v.(string))
}
if v, ok := d.GetOk("tags"); ok {
tags := []*servicecatalog.Tag{}
t := v.(map[string]interface{})
for k, v := range t {
tag := servicecatalog.Tag{
Key: aws.String(k),
Value: aws.String(v.(string)),
}
tags = append(tags, &tag)
}
input.Tags = tags
}
log.Printf("[DEBUG] Creating Service Catalog Portfolio: %#v", input)
resp, err := conn.CreatePortfolio(&input)
if err != nil {
return fmt.Errorf("Creating Service Catalog Portfolio failed: %s", err.Error())
}
d.SetId(*resp.PortfolioDetail.Id)
return resourceAwsServiceCatalogPortfolioRead(d, meta)
}
func resourceAwsServiceCatalogPortfolioRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).scconn
input := servicecatalog.DescribePortfolioInput{
AcceptLanguage: aws.String("en"),
}
input.Id = aws.String(d.Id())
log.Printf("[DEBUG] Reading Service Catalog Portfolio: %#v", input)
resp, err := conn.DescribePortfolio(&input)
if err != nil {
if scErr, ok := err.(awserr.Error); ok && scErr.Code() == "ResourceNotFoundException" {
log.Printf("[WARN] Service Catalog Portfolio %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Reading ServiceCatalog Portfolio '%s' failed: %s", *input.Id, err.Error())
}
portfolioDetail := resp.PortfolioDetail
if err := d.Set("created_time", portfolioDetail.CreatedTime.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting created_time: %s", err)
}
d.Set("arn", portfolioDetail.ARN)
d.Set("description", portfolioDetail.Description)
d.Set("name", portfolioDetail.DisplayName)
d.Set("provider_name", portfolioDetail.ProviderName)
tags := map[string]string{}
for _, tag := range resp.Tags {
tags[*tag.Key] = *tag.Value
}
d.Set("tags", tags)
return nil
}
func resourceAwsServiceCatalogPortfolioUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).scconn
input := servicecatalog.UpdatePortfolioInput{
AcceptLanguage: aws.String("en"),
Id: aws.String(d.Id()),
}
if d.HasChange("name") {
v, _ := d.GetOk("name")
input.DisplayName = aws.String(v.(string))
}
if d.HasChange("accept_language") {
v, _ := d.GetOk("accept_language")
input.AcceptLanguage = aws.String(v.(string))
}
if d.HasChange("description") {
v, _ := d.GetOk("description")
input.Description = aws.String(v.(string))
}
if d.HasChange("provider_name") {
v, _ := d.GetOk("provider_name")
input.ProviderName = aws.String(v.(string))
}
if d.HasChange("tags") {
currentTags, requiredTags := d.GetChange("tags")
log.Printf("[DEBUG] Current Tags: %#v", currentTags)
log.Printf("[DEBUG] Required Tags: %#v", requiredTags)
tagsToAdd, tagsToRemove := tagUpdates(requiredTags.(map[string]interface{}), currentTags.(map[string]interface{}))
log.Printf("[DEBUG] Tags To Add: %#v", tagsToAdd)
log.Printf("[DEBUG] Tags To Remove: %#v", tagsToRemove)
input.AddTags = tagsToAdd
input.RemoveTags = tagsToRemove
}
log.Printf("[DEBUG] Update Service Catalog Portfolio: %#v", input)
_, err := conn.UpdatePortfolio(&input)
if err != nil {
return fmt.Errorf("Updating Service Catalog Portfolio '%s' failed: %s", *input.Id, err.Error())
}
return resourceAwsServiceCatalogPortfolioRead(d, meta)
}
func tagUpdates(requriedTags, currentTags map[string]interface{}) ([]*servicecatalog.Tag, []*string) {
var tagsToAdd []*servicecatalog.Tag
var tagsToRemove []*string
for rk, rv := range requriedTags {
addTag := true
for ck, cv := range currentTags {
if (rk == ck) && (rv.(string) == cv.(string)) {
addTag = false
}
}
if addTag {
tag := &servicecatalog.Tag{Key: aws.String(rk), Value: aws.String(rv.(string))}
tagsToAdd = append(tagsToAdd, tag)
}
}
for ck, cv := range currentTags {
removeTag := true
for rk, rv := range requriedTags {
if (rk == ck) && (rv.(string) == cv.(string)) {
removeTag = false
}
}
if removeTag {
tagsToRemove = append(tagsToRemove, aws.String(ck))
}
}
return tagsToAdd, tagsToRemove
}
func resourceAwsServiceCatalogPortfolioDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).scconn
input := servicecatalog.DeletePortfolioInput{}
input.Id = aws.String(d.Id())
log.Printf("[DEBUG] Delete Service Catalog Portfolio: %#v", input)
_, err := conn.DeletePortfolio(&input)
if err != nil {
return fmt.Errorf("Deleting Service Catalog Portfolio '%s' failed: %s", *input.Id, err.Error())
}
return nil
}