Skip to content

Commit

Permalink
Merge pull request #182 from Cox-Automotive/errorRefactor
Browse files Browse the repository at this point in the history
Update errors with AlksError
  • Loading branch information
codykoelemay authored Oct 27, 2022
2 parents bd1875b + f236184 commit db10a70
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 160 deletions.
5 changes: 1 addition & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ func generateNewClient(c *Config, client *alks.Client) (*alks.Client, error) {
return nil, err
}

newClient, err := alks.NewSTSClient(c.URL, newCreds.AccessKey, newCreds.SecretKey, newCreds.SessionToken)
if err != nil {
return nil, err
}
newClient, _ := alks.NewSTSClient(c.URL, newCreds.AccessKey, newCreds.SecretKey, newCreds.SessionToken)

// 5. Return this new client for provider
return newClient, nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/Cox-Automotive/terraform-provider-alks
go 1.18

require (
github.com/Cox-Automotive/alks-go v0.0.0-20221019181202-84b27abafb6b
github.com/Cox-Automotive/alks-go v0.0.0-20221026220646-c20da5c3cb3a
github.com/aws/aws-sdk-go v1.31.15
github.com/hashicorp/terraform-plugin-sdk/v2 v2.21.0
github.com/mitchellh/go-homedir v1.1.0
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ github.com/Cox-Automotive/alks-go v0.0.0-20221010204605-136b6e9b6530 h1:8j3NYoLn
github.com/Cox-Automotive/alks-go v0.0.0-20221010204605-136b6e9b6530/go.mod h1:jJNgXthl59Vt2tJHSC3WZ0vlopV9xqdclfQuLgwHjOw=
github.com/Cox-Automotive/alks-go v0.0.0-20221019181202-84b27abafb6b h1:9Ey7kdUL+/f5EY2KOpTawWMw4P7fhZxNmo8gXIuBQzw=
github.com/Cox-Automotive/alks-go v0.0.0-20221019181202-84b27abafb6b/go.mod h1:jJNgXthl59Vt2tJHSC3WZ0vlopV9xqdclfQuLgwHjOw=
github.com/Cox-Automotive/alks-go v0.0.0-20221025180450-2e58fb1fb8a5 h1:uUI/XCe1bc9nq3d6jK05TsxOmCf8sLLOX/L3guAYdtg=
github.com/Cox-Automotive/alks-go v0.0.0-20221025180450-2e58fb1fb8a5/go.mod h1:jJNgXthl59Vt2tJHSC3WZ0vlopV9xqdclfQuLgwHjOw=
github.com/Cox-Automotive/alks-go v0.0.0-20221025210209-c92be9283c74 h1:ceg7E2BWn1HNGUZx2K8XQ3uQjT3phYuoeSGUv8Rqcdc=
github.com/Cox-Automotive/alks-go v0.0.0-20221025210209-c92be9283c74/go.mod h1:jJNgXthl59Vt2tJHSC3WZ0vlopV9xqdclfQuLgwHjOw=
github.com/Cox-Automotive/alks-go v0.0.0-20221026190610-eec2e9aedb00 h1:0lV5ATCvq93nrKPm4tD9HpgaAMhfaz2I31Xb6VbIpj8=
github.com/Cox-Automotive/alks-go v0.0.0-20221026190610-eec2e9aedb00/go.mod h1:jJNgXthl59Vt2tJHSC3WZ0vlopV9xqdclfQuLgwHjOw=
github.com/Cox-Automotive/alks-go v0.0.0-20221026220646-c20da5c3cb3a h1:EJJqr3Dg89DRi7eqtD9OX0XOqabndVqK0r9cy8Y7aq0=
github.com/Cox-Automotive/alks-go v0.0.0-20221026220646-c20da5c3cb3a/go.mod h1:jJNgXthl59Vt2tJHSC3WZ0vlopV9xqdclfQuLgwHjOw=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
Expand Down
12 changes: 8 additions & 4 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package main

import (
"errors"
"fmt"

"github.com/Cox-Automotive/alks-go"
)

func validateIAMEnabled(client *alks.Client) error {
func validateIAMEnabled(client *alks.Client) *alks.AlksError {
// Validate STS for IAM active.
resp, err := client.GetMyLoginRole()
if err != nil {
return err
}

if !resp.LoginRole.IamKeyActive {
return errors.New("uh oh! You're using the " + resp.LoginRole.Role + " role which is not IAM active. " +
"Please instead use one of the following roles: Admin, IAMAdmin, LabAdmin, or a Machine Identity")
return &alks.AlksError{
StatusCode: 0,
RequestId: "",
Err: fmt.Errorf("uh oh! You're using the " + resp.LoginRole.Role + " role which is not IAM active. " +
"Please instead use one of the following roles: Admin, IAMAdmin, LabAdmin, or a Machine Identity"),
}
}

return nil
Expand Down
14 changes: 7 additions & 7 deletions resource_alks_iamrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func resourceAlksIamRoleCreate(ctx context.Context, d *schema.ResourceData, meta

resp, err := client.CreateIamRole(options)
if err != nil {
return diag.FromErr(err.Err)
return diag.FromErr(err)
}

d.SetId(resp.RoleName)
Expand All @@ -174,7 +174,7 @@ func resourceAlksIamRoleDelete(ctx context.Context, d *schema.ResourceData, meta
}

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

return nil
Expand All @@ -199,7 +199,7 @@ func resourceAlksIamRoleRead(ctx context.Context, d *schema.ResourceData, meta i
//If error is 404, RoleNotFound, we log it and let terraform decide how to handle it.
//All other errors cause a failure
if err.StatusCode == 404 {
log.Printf("[Error] %s", err.Err)
log.Printf("[Error] %s", err.Error())
d.SetId("")
return nil
}
Expand Down Expand Up @@ -283,13 +283,13 @@ func updateAlksAccess(d *schema.ResourceData, meta interface{}) error {
if alksAccess {
_, err := client.AddRoleMachineIdentity(roleArn)
if err != nil {
return err.Err
return err
}
} else {
// delete the machine identity
_, err := client.DeleteRoleMachineIdentity(roleArn)
if err != nil {
return err.Err
return err
}
}
return nil
Expand All @@ -308,7 +308,7 @@ func updateIamRoleTags(d *schema.ResourceData, meta interface{}) error {
foundRole, err := client.GetIamRole(d.Id())

if err != nil {
return err.Err
return err
}

existingTags := tagSliceToMap(foundRole.Tags)
Expand All @@ -324,7 +324,7 @@ func updateIamRoleTags(d *schema.ResourceData, meta interface{}) error {
}

if _, err := client.UpdateIamRole(&options); err != nil {
return err.Err
return err
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion resource_alks_iamtrustrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func resourceAlksIamTrustRoleCreate(ctx context.Context, d *schema.ResourceData,
resp, err = client.CreateIamTrustRole(options)
if err != nil {
if strings.Contains(err.Error(), "Role already exists") || strings.Contains(err.Error(), "Instance profile exists") {
return resource.NonRetryableError(err.Err)
return resource.NonRetryableError(err)
}

// Amazon IAM utilizes an eventual consistency model:
Expand Down
10 changes: 5 additions & 5 deletions resource_alks_ltk.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func resourceAlksLtkCreate(ctx context.Context, d *schema.ResourceData, meta int

resp, err := client.CreateIamUser(options)
if err != nil {
return diag.FromErr(err.Err)
return diag.FromErr(err)
}

d.SetId(iamUsername)
Expand Down Expand Up @@ -103,11 +103,11 @@ func resourceAlksLtkRead(ctx context.Context, d *schema.ResourceData, meta inter
//If error is 404, UserNotFound, we log it and let terraform decide how to handle it.
//All other errors cause a failure
if err.StatusCode == 404 {
log.Printf("[Error] %s", err.Err)
log.Printf("[Error] %s", err)
d.SetId("")
return nil
}
return diag.FromErr(err.Err)
return diag.FromErr(err)
}

log.Printf("[INFO] alks_ltk.id: %v", d.Id())
Expand Down Expand Up @@ -159,7 +159,7 @@ func resourceAlksLtkDelete(ctx context.Context, d *schema.ResourceData, meta int
}

if _, err := client.DeleteIamUser(d.Id()); err != nil {
return diag.FromErr(err.Err)
return diag.FromErr(err)
}

return nil
Expand Down Expand Up @@ -194,7 +194,7 @@ func updateUserTags(d *schema.ResourceData, meta interface{}) error {
}

if _, err := client.UpdateIamUser(&options); err != nil {
return err.Err
return err
}
return nil
}
2 changes: 2 additions & 0 deletions vendor/github.com/Cox-Automotive/alks-go/alks_error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db10a70

Please sign in to comment.