Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_licensemanager: Add aws_licensemanager_association resource #6926

Merged
merged 4 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ func Provider() terraform.ResourceProvider {
"aws_lambda_permission": resourceAwsLambdaPermission(),
"aws_launch_configuration": resourceAwsLaunchConfiguration(),
"aws_launch_template": resourceAwsLaunchTemplate(),
"aws_licensemanager_association": resourceAwsLicenseManagerAssociation(),
"aws_licensemanager_license_configuration": resourceAwsLicenseManagerLicenseConfiguration(),
"aws_lightsail_domain": resourceAwsLightsailDomain(),
"aws_lightsail_instance": resourceAwsLightsailInstance(),
Expand Down
48 changes: 48 additions & 0 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,20 @@ func resourceAwsLaunchTemplate() *schema.Resource {
Optional: true,
},

"license_specification": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"license_configuration_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
},
},
},

"monitoring": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -627,6 +641,10 @@ func resourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) err
return err
}

if err := d.Set("license_specification", getLicenseSpecifications(ltData.LicenseSpecifications)); err != nil {
return err
}

if err := d.Set("monitoring", getMonitoring(ltData.Monitoring)); err != nil {
return err
}
Expand Down Expand Up @@ -830,6 +848,16 @@ func getInstanceMarketOptions(m *ec2.LaunchTemplateInstanceMarketOptions) []inte
return s
}

func getLicenseSpecifications(licenseSpecifications []*ec2.LaunchTemplateLicenseConfiguration) []map[string]interface{} {
var s []map[string]interface{}
for _, v := range licenseSpecifications {
s = append(s, map[string]interface{}{
"license_configuration_arn": aws.StringValue(v.LicenseConfigurationArn),
})
}
return s
}

func getMonitoring(m *ec2.LaunchTemplatesMonitoring) []interface{} {
s := []interface{}{}
if m != nil {
Expand Down Expand Up @@ -1035,6 +1063,16 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
}
}

if v, ok := d.GetOk("license_specification"); ok {
var licenseSpecifications []*ec2.LaunchTemplateLicenseConfigurationRequest
lsList := v.(*schema.Set).List()

for _, ls := range lsList {
licenseSpecifications = append(licenseSpecifications, readLicenseSpecificationFromConfig(ls.(map[string]interface{})))
}
opts.LicenseSpecifications = licenseSpecifications
}

if v, ok := d.GetOk("monitoring"); ok {
m := v.([]interface{})
if len(m) > 0 {
Expand Down Expand Up @@ -1329,6 +1367,16 @@ func readInstanceMarketOptionsFromConfig(imo map[string]interface{}) (*ec2.Launc
return instanceMarketOptions, nil
}

func readLicenseSpecificationFromConfig(ls map[string]interface{}) *ec2.LaunchTemplateLicenseConfigurationRequest {
licenseSpecification := &ec2.LaunchTemplateLicenseConfigurationRequest{}

if v, ok := ls["license_configuration_arn"].(string); ok && v != "" {
licenseSpecification.LicenseConfigurationArn = aws.String(v)
}

return licenseSpecification
}

func readPlacementFromConfig(p map[string]interface{}) *ec2.LaunchTemplatePlacementRequest {
placement := &ec2.LaunchTemplatePlacementRequest{}

Expand Down
38 changes: 38 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,27 @@ func TestAccAWSLaunchTemplate_instanceMarketOptions(t *testing.T) {
})
}

func TestAccAWSLaunchTemplate_licenseSpecification(t *testing.T) {
var template ec2.LaunchTemplate
resName := "aws_launch_template.example"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateConfig_licenseSpecification(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resName, &template),
resource.TestCheckResourceAttr(resName, "license_specification.#", "1"),
),
},
},
})
}

func testAccCheckAWSLaunchTemplateExists(n string, t *ec2.LaunchTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -859,6 +880,23 @@ resource "aws_launch_template" "foo" {
`, instanceType, rName, cpuCredits)
}

func testAccAWSLaunchTemplateConfig_licenseSpecification(rInt int) string {
return fmt.Sprintf(`
resource "aws_licensemanager_license_configuration" "example" {
name = "Example"
license_counting_type = "vCPU"
}

resource "aws_launch_template" "example" {
name = "foo_%d"

license_specification {
license_configuration_arn = "${aws_licensemanager_license_configuration.example.id}"
}
}
`, rInt)
}

const testAccAWSLaunchTemplateConfig_networkInterface = `
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
Expand Down
146 changes: 146 additions & 0 deletions aws/resource_aws_licensemanager_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
Loading