forked from hashicorp/terraform-provider-aws
/
resource_aws_licensemanager_association.go
146 lines (116 loc) · 4.25 KB
/
resource_aws_licensemanager_association.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
package aws
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/licensemanager"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsLicenseManagerAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLicenseManagerAssociationCreate,
Read: resourceAwsLicenseManagerAssociationRead,
Delete: resourceAwsLicenseManagerAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"resource_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"license_configuration_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
},
}
}
func resourceAwsLicenseManagerAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn
resourceArn := d.Get("resource_arn").(string)
licenseConfigurationArn := d.Get("license_configuration_arn").(string)
opts := &licensemanager.UpdateLicenseSpecificationsForResourceInput{
AddLicenseSpecifications: []*licensemanager.LicenseSpecification{{
LicenseConfigurationArn: aws.String(licenseConfigurationArn),
}},
ResourceArn: aws.String(resourceArn),
}
log.Printf("[DEBUG] License Manager association: %s", opts)
_, err := conn.UpdateLicenseSpecificationsForResource(opts)
if err != nil {
return fmt.Errorf("Error creating License Manager association: %s", err)
}
d.SetId(fmt.Sprintf("%s,%s", resourceArn, licenseConfigurationArn))
return resourceAwsLicenseManagerAssociationRead(d, meta)
}
func resourceAwsLicenseManagerAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn
resourceArn, licenseConfigurationArn, err := resourceAwsLicenseManagerAssociationParseId(d.Id())
if err != nil {
return err
}
licenseSpecification, err := resourceAwsLicenseManagerAssociationFindSpecification(conn, resourceArn, licenseConfigurationArn)
if err != nil {
return fmt.Errorf("Error reading License Manager association: %s", err)
}
if licenseSpecification == nil {
log.Printf("[WARN] License Manager association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
d.Set("resource_arn", resourceArn)
d.Set("license_configuration_arn", licenseConfigurationArn)
return nil
}
func resourceAwsLicenseManagerAssociationFindSpecification(conn *licensemanager.LicenseManager, resourceArn, licenseConfigurationArn string) (*licensemanager.LicenseSpecification, error) {
opts := &licensemanager.ListLicenseSpecificationsForResourceInput{
ResourceArn: aws.String(resourceArn),
}
for {
resp, err := conn.ListLicenseSpecificationsForResource(opts)
if err != nil {
return nil, err
}
for _, licenseSpecification := range resp.LicenseSpecifications {
if aws.StringValue(licenseSpecification.LicenseConfigurationArn) == licenseConfigurationArn {
return licenseSpecification, nil
}
}
if len(resp.LicenseSpecifications) == 0 || resp.NextToken == nil {
return nil, nil
}
opts.NextToken = resp.NextToken
}
}
func resourceAwsLicenseManagerAssociationParseId(id string) (string, string, error) {
parts := strings.SplitN(id, ",", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("Expected License Manager Association ID in the form resource_arn,license_configuration_arn - received: %s", id)
}
return parts[0], parts[1], nil
}
func resourceAwsLicenseManagerAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).licensemanagerconn
resourceArn, licenseConfigurationArn, err := resourceAwsLicenseManagerAssociationParseId(d.Id())
if err != nil {
return err
}
opts := &licensemanager.UpdateLicenseSpecificationsForResourceInput{
RemoveLicenseSpecifications: []*licensemanager.LicenseSpecification{{
LicenseConfigurationArn: aws.String(licenseConfigurationArn),
}},
ResourceArn: aws.String(resourceArn),
}
log.Printf("[DEBUG] License Manager association: %s", opts)
_, err = conn.UpdateLicenseSpecificationsForResource(opts)
if err != nil {
return fmt.Errorf("Error deleting License Manager association: %s", err)
}
return nil
}