|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + gitlab "github.com/xanzy/go-gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = registerResource("gitlab_user_sshkey", func() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + CreateContext: resourceGitlabUserSSHKeyCreate, |
| 17 | + ReadContext: resourceGitlabUserSSHKeyRead, |
| 18 | + UpdateContext: resourceGitlabUserSSHKeyUpdate, |
| 19 | + DeleteContext: resourceGitlabUserSSHKeyDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + StateContext: resourceGitlabUserSSHKeyImporter, |
| 22 | + }, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "title": { |
| 26 | + Description: "The title of the ssh key.", |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "key": { |
| 31 | + Description: "The ssh key.", |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + }, |
| 35 | + "created_at": { |
| 36 | + Description: "Create time.", |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "user_id": { |
| 41 | + Description: "The id of the user to add the ssh key to.", |
| 42 | + Type: schema.TypeInt, |
| 43 | + ForceNew: true, |
| 44 | + Required: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | +}) |
| 49 | + |
| 50 | +func resourceGitlabUserSSHKeyImporter(ctx context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { |
| 51 | + s := strings.Split(d.Id(), ":") |
| 52 | + if len(s) != 2 { |
| 53 | + d.SetId("") |
| 54 | + return nil, fmt.Errorf("Invalid SSH Key import format; expected '{user_id}:{key_id}'") |
| 55 | + } |
| 56 | + |
| 57 | + userID, err := strconv.Atoi(s[0]) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("Invalid SSH Key import format; expected '{user_id}:{key_id}'") |
| 60 | + } |
| 61 | + |
| 62 | + d.Set("user_id", userID) |
| 63 | + d.SetId(s[1]) |
| 64 | + |
| 65 | + return []*schema.ResourceData{d}, nil |
| 66 | +} |
| 67 | + |
| 68 | +func resourceGitlabUserSSHKeySetToState(d *schema.ResourceData, key *gitlab.SSHKey) { |
| 69 | + d.Set("title", key.Title) |
| 70 | + d.Set("key", key.Key) |
| 71 | + d.Set("created_at", key.CreatedAt.String()) |
| 72 | +} |
| 73 | + |
| 74 | +func resourceGitlabUserSSHKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 75 | + client := meta.(*gitlab.Client) |
| 76 | + |
| 77 | + userID := d.Get("user_id").(int) |
| 78 | + |
| 79 | + options := &gitlab.AddSSHKeyOptions{ |
| 80 | + Title: gitlab.String(d.Get("title").(string)), |
| 81 | + Key: gitlab.String(d.Get("key").(string)), |
| 82 | + } |
| 83 | + |
| 84 | + key, _, err := client.Users.AddSSHKeyForUser(userID, options, gitlab.WithContext(ctx)) |
| 85 | + if err != nil { |
| 86 | + return diag.FromErr(err) |
| 87 | + } |
| 88 | + |
| 89 | + d.SetId(fmt.Sprintf("%d", key.ID)) |
| 90 | + |
| 91 | + return resourceGitlabUserSSHKeyRead(ctx, d, meta) |
| 92 | +} |
| 93 | + |
| 94 | +func resourceGitlabUserSSHKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 95 | + client := meta.(*gitlab.Client) |
| 96 | + |
| 97 | + id, _ := strconv.Atoi(d.Id()) |
| 98 | + userID := d.Get("user_id").(int) |
| 99 | + |
| 100 | + keys, _, err := client.Users.ListSSHKeysForUser(userID, &gitlab.ListSSHKeysForUserOptions{}, gitlab.WithContext(ctx)) |
| 101 | + if err != nil { |
| 102 | + return diag.FromErr(err) |
| 103 | + } |
| 104 | + |
| 105 | + var key *gitlab.SSHKey |
| 106 | + |
| 107 | + for _, k := range keys { |
| 108 | + if k.ID == id { |
| 109 | + key = k |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if key == nil { |
| 115 | + return diag.Errorf("Could not find sshkey %d for user %d", id, userID) |
| 116 | + } |
| 117 | + |
| 118 | + resourceGitlabUserSSHKeySetToState(d, key) |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func resourceGitlabUserSSHKeyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 123 | + if d := resourceGitlabUserSSHKeyDelete(ctx, d, meta); d.HasError() { |
| 124 | + return d |
| 125 | + } |
| 126 | + if d := resourceGitlabUserSSHKeyCreate(ctx, d, meta); d.HasError() { |
| 127 | + return d |
| 128 | + } |
| 129 | + return resourceGitlabUserSSHKeyRead(ctx, d, meta) |
| 130 | +} |
| 131 | + |
| 132 | +func resourceGitlabUserSSHKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 133 | + client := meta.(*gitlab.Client) |
| 134 | + |
| 135 | + id, _ := strconv.Atoi(d.Id()) |
| 136 | + userID := d.Get("user_id").(int) |
| 137 | + |
| 138 | + if _, err := client.Users.DeleteSSHKeyForUser(userID, id, gitlab.WithContext(ctx)); err != nil { |
| 139 | + return diag.FromErr(err) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments