-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from Cox-Automotive/develop
Develop
- Loading branch information
Showing
12 changed files
with
375 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,6 @@ glide.lock | |
*.iml | ||
|
||
# VSCode | ||
.vscode | ||
.vscode | ||
.DS_Store | ||
.terraform/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package main | ||
|
||
import ( | ||
alks "github.com/Cox-Automotive/alks-go" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"log" | ||
) | ||
|
||
func resourceAlksLtk() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAlksLtkCreate, | ||
Read: resourceAlksLtkRead, | ||
Delete: resourceAlksLtkDelete, | ||
Exists: resourceAlksLtkExists, | ||
Importer: &schema.ResourceImporter{ | ||
// Terraform provided importer | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
SchemaVersion: 1, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"iam_username": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"iam_user_arn": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"access_key": &schema.Schema{ | ||
Sensitive: true, | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"secret_key": &schema.Schema{ | ||
Sensitive: true, | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAlksLtkCreate(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[INFO] ALKS LTK User Create") | ||
|
||
var iamUsername = d.Get("iam_username").(string) | ||
|
||
client := meta.(*alks.Client) | ||
resp, err := client.CreateLongTermKey(iamUsername) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(iamUsername) | ||
_ = d.Set("iam_user_arn", resp.IAMUserArn) | ||
_ = d.Set("access_key", resp.AccessKey) | ||
_ = d.Set("secret_key", resp.SecretKey) | ||
|
||
log.Printf("[INFO] alks_ltk.id: %v", d.Id()) | ||
|
||
return resourceAlksLtkRead(d, meta) | ||
} | ||
|
||
func resourceAlksLtkRead(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[INFO] ALKS LTK User Read") | ||
|
||
client := meta.(*alks.Client) | ||
resp, err := client.GetLongTermKey(d.Id()) | ||
|
||
if err != nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
log.Printf("[INFO] alks_ltk.id: %v", d.Id()) | ||
|
||
_ = d.Set("iam_username", resp.UserName) | ||
_ = d.Set("access_key", resp.AccessKeyID) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAlksLtkDelete(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[INFO] ALKS LTK User Delete") | ||
|
||
client := meta.(*alks.Client) | ||
_, err := client.DeleteLongTermKey(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAlksLtkExists(d *schema.ResourceData, meta interface{}) (bool, error) { | ||
log.Printf("[INFO] ALKS LTK User Exists") | ||
|
||
client := meta.(*alks.Client) | ||
resp, err := client.GetLongTermKey(d.Id()) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// We can get a 200, but an empty string so this is the condition to check for. | ||
if len(resp.LongTermKey.UserName) == 0 { | ||
return false, nil | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
alks "github.com/Cox-Automotive/alks-go" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"testing" | ||
) | ||
|
||
func TestAlksLTKCreate(t *testing.T) { | ||
var resp alks.CreateLongTermKeyResponse | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAlksLtkDestroy(&resp), | ||
Steps: []resource.TestStep{ | ||
// Create the resource | ||
resource.TestStep{ | ||
Config: testAlksLTKCreateConfig, | ||
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("alks_ltk.foo", "iam_username", "TEST_LTK_USER")), | ||
}, | ||
// Update the resource | ||
resource.TestStep{ | ||
Config: testAlksLTKUpdateConfig, | ||
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("alks_ltk.foo", "iam_username", "TEST_LTK_USER_2")), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAlksLtkDestroy(ltk *alks.CreateLongTermKeyResponse) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*alks.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "alks_ltk" { | ||
continue | ||
} | ||
|
||
resp, err := client.GetLongTermKey(rs.Primary.ID) | ||
if resp != nil { | ||
return fmt.Errorf("long term key still exists: %#v (%v)", resp, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAlksLTKCreateConfig = ` | ||
resource "alks_ltk" "foo" { | ||
iam_username = "TEST_LTK_USER" | ||
} | ||
` | ||
|
||
const testAlksLTKUpdateConfig = ` | ||
resource "alks_ltk" "foo" { | ||
iam_username = "TEST_LTK_USER_2" | ||
} | ||
` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.