Skip to content

Commit

Permalink
Merge pull request integrations#119 from terraform-providers/f-ssh-key
Browse files Browse the repository at this point in the history
New Resource: github_user_ssh_key
  • Loading branch information
radeksimko authored Aug 10, 2018
2 parents bd367b5 + df60b9b commit fafdac5
Show file tree
Hide file tree
Showing 16 changed files with 1,607 additions and 0 deletions.
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Provider() terraform.ResourceProvider {
"github_team": resourceGithubTeam(),
"github_team_membership": resourceGithubTeamMembership(),
"github_team_repository": resourceGithubTeamRepository(),
"github_user_ssh_key": resourceGithubUserSshKey(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
2 changes: 2 additions & 0 deletions github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-tls/tls"
)

var testUser string = os.Getenv("GITHUB_TEST_USER")
Expand All @@ -24,6 +25,7 @@ func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"github": testAccProvider,
"tls": tls.Provider(),
}
}

Expand Down
95 changes: 95 additions & 0 deletions github/resource_github_user_ssh_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package github

import (
"context"
"strconv"
"strings"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceGithubUserSshKey() *schema.Resource {
return &schema.Resource{
Create: resourceGithubUserSshKeyCreate,
Read: resourceGithubUserSshKeyRead,
Delete: resourceGithubUserSshKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"title": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: func(k, oldV, newV string, d *schema.ResourceData) bool {
newTrimmed := strings.TrimSpace(newV)
return oldV == newTrimmed
},
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceGithubUserSshKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

title := d.Get("title").(string)
key := d.Get("key").(string)

userKey, _, err := client.Users.CreateKey(context.TODO(), &github.Key{
Title: &title,
Key: &key,
})
if err != nil {
return err
}

d.SetId(strconv.FormatInt(*userKey.ID, 10))

return resourceGithubUserSshKeyRead(d, meta)
}

func resourceGithubUserSshKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return unconvertibleIdErr(d.Id(), err)
}

key, _, err := client.Users.GetKey(context.TODO(), id)
if err != nil {
d.SetId("")
return nil
}

d.Set("title", key.Title)
d.Set("key", key.Key)
d.Set("url", key.URL)

return nil
}

func resourceGithubUserSshKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return unconvertibleIdErr(d.Id(), err)
}

_, err = client.Users.DeleteKey(context.TODO(), id)

return err
}
121 changes: 121 additions & 0 deletions github/resource_github_user_ssh_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package github

import (
"context"
"fmt"
"regexp"
"strconv"
"testing"

"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccGithubUserSshKey_basic(t *testing.T) {
var key github.Key
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
title := fmt.Sprintf("tf-acc-test-%s", randString)
keyRe := regexp.MustCompile("^ecdsa-sha2-nistp384 ")
urlRe := regexp.MustCompile("^https://api.github.com/[a-z0-9]+/keys/")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubUserSshKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubUserSshKeyConfig(title),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubUserSshKeyExists("github_user_ssh_key.test", &key),
resource.TestCheckResourceAttr("github_user_ssh_key.test", "title", title),
resource.TestMatchResourceAttr("github_user_ssh_key.test", "key", keyRe),
resource.TestMatchResourceAttr("github_user_ssh_key.test", "url", urlRe),
),
},
},
})
}

func TestAccGithubUserSshKey_importBasic(t *testing.T) {
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
title := fmt.Sprintf("tf-acc-test-%s", randString)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubUserSshKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubUserSshKeyConfig(title),
},
{
ResourceName: "github_user_ssh_key.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckGithubUserSshKeyExists(n string, key *github.Key) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}

id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return unconvertibleIdErr(rs.Primary.ID, err)
}

org := testAccProvider.Meta().(*Organization)
receivedKey, _, err := org.client.Users.GetKey(context.TODO(), id)
if err != nil {
return err
}
*key = *receivedKey
return nil
}
}

func testAccCheckGithubUserSshKeyDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Organization).client

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

id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return unconvertibleIdErr(rs.Primary.ID, err)
}

_, resp, err := conn.Users.GetKey(context.TODO(), id)
if err == nil {
return fmt.Errorf("SSH key %s still exists", rs.Primary.ID)
}
if resp.StatusCode != 404 {
return err
}
return nil
}
return nil
}

func testAccGithubUserSshKeyConfig(title string) string {
return fmt.Sprintf(`
resource "github_user_ssh_key" "test" {
title = "%s"
key = "${tls_private_key.test.public_key_openssh}"
}
resource "tls_private_key" "test" {
algorithm = "ECDSA"
ecdsa_curve = "P384"
}
`, title)
}
Loading

0 comments on commit fafdac5

Please sign in to comment.