Skip to content

Commit

Permalink
Merge pull request #138 from Cox-Automotive/refactorDeprecatedFuncs
Browse files Browse the repository at this point in the history
Refactor Deprecated Functions
  • Loading branch information
amagana3 authored Jul 12, 2021
2 parents ae7c36e + 055b8e9 commit 8c1f186
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 149 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/ linguist-generated=true
116 changes: 32 additions & 84 deletions resource_alks_iamrole.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package main

import (
"fmt"
"context"
"github.com/Cox-Automotive/alks-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"log"
"strings"
"time"
)

func resourceAlksIamRole() *schema.Resource {
return &schema.Resource{
Create: resourceAlksIamRoleCreate,
Read: resourceAlksIamRoleRead,
Update: resourceAlksIamRoleUpdate,
Exists: resourceAlksIamRoleExists,
Delete: resourceAlksIamRoleDelete,
CreateContext: resourceAlksIamRoleCreate,
ReadContext: resourceAlksIamRoleRead,
UpdateContext: resourceAlksIamRoleUpdate,
DeleteContext: resourceAlksIamRoleDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

SchemaVersion: 1,
MigrateState: migrateState,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -70,18 +65,13 @@ func resourceAlksIamRole() *schema.Resource {

func resourceAlksIamTrustRole() *schema.Resource {
return &schema.Resource{
Create: resourceAlksIamTrustRoleCreate,
Read: resourceAlksIamRoleRead,
Update: resourceAlksIamRoleUpdate,
Exists: resourceAlksIamRoleExists,
Delete: resourceAlksIamRoleDelete,
CreateContext: resourceAlksIamTrustRoleCreate,
ReadContext: resourceAlksIamRoleRead,
UpdateContext: resourceAlksIamRoleUpdate,
DeleteContext: resourceAlksIamRoleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},

SchemaVersion: 1,
MigrateState: migrateState,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -119,7 +109,7 @@ func resourceAlksIamTrustRole() *schema.Resource {
}
}

func resourceAlksIamRoleCreate(d *schema.ResourceData, meta interface{}) error {
func resourceAlksIamRoleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS IAM Role Create")

var roleName = d.Get("name").(string)
Expand All @@ -135,23 +125,23 @@ func resourceAlksIamRoleCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*alks.Client)
if err := validateIAMEnabled(client); err != nil {
return err
return diag.FromErr(err)
}

resp, err := client.CreateIamRole(roleName, roleType, templateFields, incDefPol, enableAlksAccess)
if err != nil {
return err
return diag.FromErr(err)
}

d.SetId(resp.RoleName)
_ = d.Set("role_added_to_ip", resp.RoleAddedToIP)

log.Printf("[INFO] alks_iamrole.id: %v", d.Id())

return resourceAlksIamRoleRead(d, meta)
return resourceAlksIamRoleRead(ctx, d, meta)
}

func resourceAlksIamTrustRoleCreate(d *schema.ResourceData, meta interface{}) error {
func resourceAlksIamTrustRoleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS IAM Trust Role Create")

var roleName = d.Get("name").(string)
Expand All @@ -161,7 +151,7 @@ func resourceAlksIamTrustRoleCreate(d *schema.ResourceData, meta interface{}) er

client := meta.(*alks.Client)
if err := validateIAMEnabled(client); err != nil {
return err
return diag.FromErr(err)
}

var resp *alks.IamRoleResponse
Expand All @@ -182,7 +172,7 @@ func resourceAlksIamTrustRoleCreate(d *schema.ResourceData, meta interface{}) er
})

if err != nil {
return err
return diag.FromErr(err)
}

response := *resp
Expand All @@ -192,56 +182,39 @@ func resourceAlksIamTrustRoleCreate(d *schema.ResourceData, meta interface{}) er

log.Printf("[INFO] alks_iamtrustrole.id: %v", d.Id())

return resourceAlksIamRoleRead(d, meta)
return resourceAlksIamRoleRead(ctx, d, meta)
}

func resourceAlksIamRoleDelete(d *schema.ResourceData, meta interface{}) error {
func resourceAlksIamRoleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS IAM Role Delete")

client := meta.(*alks.Client)
if err := validateIAMEnabled(client); err != nil {
return err
return diag.FromErr(err)
}

if err := client.DeleteIamRole(d.Id()); err != nil {
return err
return diag.FromErr(err)
}

return nil
}

func resourceAlksIamRoleExists(d *schema.ResourceData, meta interface{}) (b bool, e error) {
log.Printf("[INFO] ALKS IAM Role Exists")
func resourceAlksIamRoleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS IAM Role Read")

client := meta.(*alks.Client)

foundRole, err := client.GetIamRole(d.Id())

if err != nil {
// TODO: Clean-up this logic, likely by improving the error responses from `alks-go`
if strings.Contains(err.Error(), "Role not found") {
return false, nil
}

return false, err
}

if foundRole == nil {
return false, nil
// Check if role exists.
if d.Id() == "" || d.Id() == "none" {
return nil
}

return true, nil
}

func resourceAlksIamRoleRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] ALKS IAM Role Read")

client := meta.(*alks.Client)
foundRole, err := client.GetIamRole(d.Id())

if err != nil {
d.SetId("")
return err
return nil
}

log.Printf("[INFO] alks_iamrole.id %v", d.Id())
Expand All @@ -259,7 +232,7 @@ func resourceAlksIamRoleRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceAlksIamRoleUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceAlksIamRoleUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS IAM Role Update")

// enable partial state mode
Expand All @@ -268,13 +241,13 @@ func resourceAlksIamRoleUpdate(d *schema.ResourceData, meta interface{}) error {
if d.HasChange("enable_alks_access") {
// try updating enable_alks_access
if err := updateAlksAccess(d, meta); err != nil {
return err
return diag.FromErr(err)
}
}

d.Partial(false)

return nil
return resourceAlksIamRoleRead(ctx, d, meta)
}

func updateAlksAccess(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -298,29 +271,4 @@ func updateAlksAccess(d *schema.ResourceData, meta interface{}) error {
}
}
return nil
}

func migrateState(version int, state *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch version {
case 0:
log.Println("[INFO] Found Instance State v0, migrating to v1")
return migrateV0toV1(state)
default:
return state, fmt.Errorf("Unrecognized version '%d' in schema for instance of ALKS IAM role '%s'", version, state.Attributes["name"])
}
}

func migrateV0toV1(state *terraform.InstanceState) (*terraform.InstanceState, error) {
if state.Empty() {
log.Println("[DEBUG] Empty InstanceState, nothing to migrate")
return state, nil
}

if _, ok := state.Attributes["enable_alks_access"]; !ok {
log.Printf("[DEBUG] Attributes before migration: %#v", state.Attributes)
state.Attributes["enable_alks_access"] = "false"
log.Printf("[DEBUG] Attributes after migration: %#v", state.Attributes)
}

return state, nil
}
}
35 changes: 2 additions & 33 deletions resource_alks_iamrole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package main

import (
"fmt"
"log"
"testing"

"github.com/Cox-Automotive/alks-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"log"
"testing"
)

func TestAccAlksIamRole_Basic(t *testing.T) {
Expand All @@ -21,8 +20,6 @@ func TestAccAlksIamRole_Basic(t *testing.T) {
{
Config: testAccCheckAlksIamRoleConfigBasic,
Check: resource.ComposeTestCheckFunc(
// testAccCheckAlksIamRoleExists("bar420", &resp),
// testAccCheckAlksIamRoleAttributes(&resp),
resource.TestCheckResourceAttr(
"alks_iamrole.foo", "name", "bar420"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -101,34 +98,6 @@ func testAccCheckAlksIamRoleDestroy(role *alks.IamRoleResponse) resource.TestChe
}
}

func testAccCheckAlksIamRoleExists(n string, role *alks.IamRoleResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

if !ok {
return fmt.Errorf("Not found: %s", n)
}

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

client := testAccProvider.Meta().(*alks.Client)

foundRole, err := client.GetIamRole(rs.Primary.ID)

if err != nil {
return err
}

if foundRole.RoleArn != rs.Primary.ID {
return fmt.Errorf("Role not found")
}

return nil
}
}

func testAccCheckAlksIamRoleAttributes(role *alks.IamRoleResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
log.Printf("[INFO] its this %v", role)
Expand Down
Loading

0 comments on commit 8c1f186

Please sign in to comment.