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_license_configuration resource #6835

Merged
merged 3 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import (
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/lexmodelbuildingservice"
"github.com/aws/aws-sdk-go/service/licensemanager"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/aws/aws-sdk-go/service/macie"
"github.com/aws/aws-sdk-go/service/mediastore"
Expand Down Expand Up @@ -222,6 +223,7 @@ type AWSClient struct {
elasticbeanstalkconn *elasticbeanstalk.ElasticBeanstalk
elastictranscoderconn *elastictranscoder.ElasticTranscoder
lambdaconn *lambda.Lambda
licensemanagerconn *licensemanager.LicenseManager
lightsailconn *lightsail.Lightsail
macieconn *macie.Macie
mqconn *mq.MQ
Expand Down Expand Up @@ -552,6 +554,7 @@ func (c *Config) Client() (interface{}, error) {
client.kmsconn = kms.New(awsKmsSess)
client.lambdaconn = lambda.New(awsLambdaSess)
client.lexmodelconn = lexmodelbuildingservice.New(sess)
client.licensemanagerconn = licensemanager.New(sess)
client.lightsailconn = lightsail.New(sess)
client.macieconn = macie.New(sess)
client.mqconn = mq.New(sess)
Expand Down
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_license_configuration": resourceAwsLicenseManagerLicenseConfiguration(),
"aws_lightsail_domain": resourceAwsLightsailDomain(),
"aws_lightsail_instance": resourceAwsLightsailInstance(),
"aws_lightsail_key_pair": resourceAwsLightsailKeyPair(),
Expand Down
189 changes: 189 additions & 0 deletions aws/resource_aws_licensemanager_license_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package aws

import (
"fmt"
"log"

"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,
gazoakley marked this conversation as resolved.
Show resolved Hide resolved
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},
},
"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)
d.Set("license_rules", flattenStringList(resp.LicenseRules))
bflad marked this conversation as resolved.
Show resolved Hide resolved
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
} else {
bflad marked this conversation as resolved.
Show resolved Hide resolved
d.SetPartial("tags")
}
}

d.Partial(false)

opts := &licensemanager.UpdateLicenseConfigurationInput{
LicenseConfigurationArn: aws.String(d.Id()),
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))
}

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, "") {
log.Printf("[WARN] License Manager license configuration (%s) not found", d.Id())
bflad marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
return fmt.Errorf("Error deleting License Manager license configuration: %s", err)
}

return nil
}
161 changes: 161 additions & 0 deletions aws/resource_aws_licensemanager_license_configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package aws

import (
"fmt"
"log"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/licensemanager"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_licensemanager_license_configuration", &resource.Sweeper{
Name: "aws_licensemanager_license_configuration",
F: testSweepLicenseManagerLicenseConfigurations,
})
}

func testSweepLicenseManagerLicenseConfigurations(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*AWSClient).licensemanagerconn

resp, err := conn.ListLicenseConfigurations(&licensemanager.ListLicenseConfigurationsInput{})

if err != nil {
bflad marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("Error retrieving License Manager license configurations: %s", err)
}

if len(resp.LicenseConfigurations) == 0 {
log.Print("[DEBUG] No License Manager license configurations to sweep")
return nil
}

for _, lc := range resp.LicenseConfigurations {
id := aws.StringValue(lc.LicenseConfigurationArn)

log.Printf("[INFO] Deleting License Manager license configuration: %s", id)

opts := &licensemanager.DeleteLicenseConfigurationInput{
LicenseConfigurationArn: aws.String(id),
}

_, err := conn.DeleteLicenseConfiguration(opts)

if err != nil {
log.Printf("[ERROR] Error deleting License Manager license configuration (%s): %s", id, err)
}
}

return nil
}

func TestAccAWSLicenseManagerLicenseConfiguration_basic(t *testing.T) {
var licenseConfiguration licensemanager.LicenseConfiguration
resourceName := "aws_licensemanager_license_configuration.example"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLicenseManagerLicenseConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccLicenseManagerLicenseConfigurationConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckLicenseManagerLicenseConfigurationExists(resourceName, &licenseConfiguration),
resource.TestCheckResourceAttr(resourceName, "name", "Example"),
resource.TestCheckResourceAttr(resourceName, "description", "Example"),
resource.TestCheckResourceAttr(resourceName, "license_count", "10"),
resource.TestCheckResourceAttr(resourceName, "license_count_hard_limit", "true"),
resource.TestCheckResourceAttr(resourceName, "license_counting_type", "Socket"),
resource.TestCheckResourceAttr(resourceName, "license_rules.#", "1"),
resource.TestCheckResourceAttr(resourceName, "license_rules.0", "#minimumSockets=3"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "barr"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckLicenseManagerLicenseConfigurationExists(resourceName string, licenseConfiguration *licensemanager.LicenseConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).licensemanagerconn
resp, err := conn.ListLicenseConfigurations(&licensemanager.ListLicenseConfigurationsInput{
LicenseConfigurationArns: [](*string){aws.String(rs.Primary.ID)},
})

if err != nil {
return fmt.Errorf("Error retrieving License Manager license configuration (%s): %s", rs.Primary.ID, err)
}

if len(resp.LicenseConfigurations) == 0 {
return fmt.Errorf("Error retrieving License Manager license configuration (%s): Not found", rs.Primary.ID)
}

*licenseConfiguration = *resp.LicenseConfigurations[0]
return nil
}
}

func testAccCheckLicenseManagerLicenseConfigurationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).licensemanagerconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_licensemanager_license_configuration" {
continue
}

// Try to find the resource
_, err := conn.GetLicenseConfiguration(&licensemanager.GetLicenseConfigurationInput{
LicenseConfigurationArn: aws.String(rs.Primary.ID),
})

if err != nil {
if isAWSErr(err, licensemanager.ErrCodeInvalidParameterValueException, "") {
continue
}
return err
}
}

return nil

}

const testAccLicenseManagerLicenseConfigurationConfig_basic = `
resource "aws_licensemanager_license_configuration" "example" {
name = "Example"
description = "Example"
license_count = 10
license_count_hard_limit = true
license_counting_type = "Socket"

license_rules = [
"#minimumSockets=3"
]

tags {
foo = "barr"
}
}
`
Loading