forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 1
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 integrations#119 from terraform-providers/f-ssh-key
New Resource: github_user_ssh_key
- Loading branch information
Showing
16 changed files
with
1,607 additions
and
0 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
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,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 | ||
} |
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,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) | ||
} |
Oops, something went wrong.