|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/go-github/v63/github" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceGithubUserSshSigningKey() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceGithubUserSshSigningKeyCreate, |
| 17 | + Read: resourceGithubUserSshSigningKeyRead, |
| 18 | + Delete: resourceGithubUserSshSigningKeyDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "title": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + ForceNew: true, |
| 28 | + Description: "A descriptive name for the new key.", |
| 29 | + }, |
| 30 | + "key": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + Description: "The public SSH key to add to your GitHub account.", |
| 35 | + DiffSuppressFunc: func(k, oldV, newV string, d *schema.ResourceData) bool { |
| 36 | + newTrimmed := strings.TrimSpace(newV) |
| 37 | + return oldV == newTrimmed |
| 38 | + }, |
| 39 | + }, |
| 40 | + "etag": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func resourceGithubUserSshSigningKeyCreate(d *schema.ResourceData, meta interface{}) error { |
| 49 | + client := meta.(*Owner).v3client |
| 50 | + |
| 51 | + title := d.Get("title").(string) |
| 52 | + key := d.Get("key").(string) |
| 53 | + ctx := context.Background() |
| 54 | + |
| 55 | + userKey, _, err := client.Users.CreateSSHSigningKey(ctx, &github.Key{ |
| 56 | + Title: github.String(title), |
| 57 | + Key: github.String(key), |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + d.SetId(strconv.FormatInt(*userKey.ID, 10)) |
| 64 | + |
| 65 | + return resourceGithubUserSshSigningKeyRead(d, meta) |
| 66 | +} |
| 67 | + |
| 68 | +func resourceGithubUserSshSigningKeyRead(d *schema.ResourceData, meta interface{}) error { |
| 69 | + client := meta.(*Owner).v3client |
| 70 | + |
| 71 | + id, err := strconv.ParseInt(d.Id(), 10, 64) |
| 72 | + if err != nil { |
| 73 | + return unconvertibleIdErr(d.Id(), err) |
| 74 | + } |
| 75 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 76 | + if !d.IsNewResource() { |
| 77 | + ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string)) |
| 78 | + } |
| 79 | + |
| 80 | + key, resp, err := client.Users.GetSSHSigningKey(ctx, id) |
| 81 | + if err != nil { |
| 82 | + if ghErr, ok := err.(*github.ErrorResponse); ok { |
| 83 | + if ghErr.Response.StatusCode == http.StatusNotModified { |
| 84 | + return nil |
| 85 | + } |
| 86 | + if ghErr.Response.StatusCode == http.StatusNotFound { |
| 87 | + log.Printf("[INFO] Removing user SSH key %s from state because it no longer exists in GitHub", |
| 88 | + d.Id()) |
| 89 | + d.SetId("") |
| 90 | + return nil |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if err = d.Set("etag", resp.Header.Get("ETag")); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + if err = d.Set("title", key.GetTitle()); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + if err = d.Set("key", key.GetKey()); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func resourceGithubUserSshSigningKeyDelete(d *schema.ResourceData, meta interface{}) error { |
| 109 | + client := meta.(*Owner).v3client |
| 110 | + |
| 111 | + id, err := strconv.ParseInt(d.Id(), 10, 64) |
| 112 | + if err != nil { |
| 113 | + return unconvertibleIdErr(d.Id(), err) |
| 114 | + } |
| 115 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 116 | + |
| 117 | + _, err = client.Users.DeleteSSHSigningKey(ctx, id) |
| 118 | + return err |
| 119 | +} |
0 commit comments