forked from hashicorp/terraform-provider-aws
/
resource_aws_licensemanager_license_configuration.go
187 lines (156 loc) · 5.54 KB
/
resource_aws_licensemanager_license_configuration.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
package aws
import (
"fmt"
"log"
"regexp"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/licensemanager"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsLicenseManagerLicenseConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLicenseManagerLicenseConfigurationCreate,
Read: resourceAwsLicenseManagerLicenseConfigurationRead,
Update: resourceAwsLicenseManagerLicenseConfigurationUpdate,
Delete: resourceAwsLicenseManagerLicenseConfigurationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
},
"license_count": {
Type: schema.TypeInt,
Optional: true,
},
"license_count_hard_limit": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"license_counting_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
licensemanager.LicenseCountingTypeVCpu,
licensemanager.LicenseCountingTypeInstance,
licensemanager.LicenseCountingTypeCore,
licensemanager.LicenseCountingTypeSocket,
}, false),
},
"license_rules": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringMatch(regexp.MustCompile("^#([^=]+)=(.+)$"), "Expected format is #RuleType=RuleValue"),
},
},
"name": {
Type: schema.TypeString,
Required: true,
},
"tags": tagsSchema(),
},
}
}
func resourceAwsLicenseManagerLicenseConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn
opts := &licensemanager.CreateLicenseConfigurationInput{
LicenseCountingType: aws.String(d.Get("license_counting_type").(string)),
Name: aws.String(d.Get("name").(string)),
}
if v, ok := d.GetOk("description"); ok {
opts.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("license_count"); ok {
opts.LicenseCount = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("license_count_hard_limit"); ok {
opts.LicenseCountHardLimit = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("license_rules"); ok {
opts.LicenseRules = expandStringList(v.([]interface{}))
}
if v, ok := d.GetOk("tags"); ok && len(v.(map[string]interface{})) > 0 {
opts.Tags = tagsFromMapLicenseManager(v.(map[string]interface{}))
}
log.Printf("[DEBUG] License Manager license configuration: %s", opts)
resp, err := conn.CreateLicenseConfiguration(opts)
if err != nil {
return fmt.Errorf("Error creating License Manager license configuration: %s", err)
}
d.SetId(*resp.LicenseConfigurationArn)
return resourceAwsLicenseManagerLicenseConfigurationRead(d, meta)
}
func resourceAwsLicenseManagerLicenseConfigurationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn
resp, err := conn.GetLicenseConfiguration(&licensemanager.GetLicenseConfigurationInput{
LicenseConfigurationArn: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, licensemanager.ErrCodeInvalidParameterValueException, "") {
log.Printf("[WARN] License Manager license configuration (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading License Manager license configuration: %s", err)
}
d.Set("description", resp.Description)
d.Set("license_count", resp.LicenseCount)
d.Set("license_count_hard_limit", resp.LicenseCountHardLimit)
d.Set("license_counting_type", resp.LicenseCountingType)
if err := d.Set("license_rules", flattenStringList(resp.LicenseRules)); err != nil {
return fmt.Errorf("error setting license_rules: %s", err)
}
d.Set("name", resp.Name)
if err := d.Set("tags", tagsToMapLicenseManager(resp.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
return nil
}
func resourceAwsLicenseManagerLicenseConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn
d.Partial(true)
if d.HasChange("tags") {
if err := setTagsLicenseManager(conn, d); err != nil {
return err
}
d.SetPartial("tags")
}
d.Partial(false)
opts := &licensemanager.UpdateLicenseConfigurationInput{
LicenseConfigurationArn: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Description: aws.String(d.Get("description").(string)),
LicenseCountHardLimit: aws.Bool(d.Get("license_count_hard_limit").(bool)),
}
if v, ok := d.GetOk("license_count"); ok {
opts.LicenseCount = aws.Int64(int64(v.(int)))
}
log.Printf("[DEBUG] License Manager license configuration: %s", opts)
_, err := conn.UpdateLicenseConfiguration(opts)
if err != nil {
return fmt.Errorf("Error updating License Manager license configuration: %s", err)
}
return resourceAwsLicenseManagerLicenseConfigurationRead(d, meta)
}
func resourceAwsLicenseManagerLicenseConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn
opts := &licensemanager.DeleteLicenseConfigurationInput{
LicenseConfigurationArn: aws.String(d.Id()),
}
_, err := conn.DeleteLicenseConfiguration(opts)
if err != nil {
if isAWSErr(err, licensemanager.ErrCodeInvalidParameterValueException, "") {
return nil
}
return fmt.Errorf("Error deleting License Manager license configuration: %s", err)
}
return nil
}