Skip to content

Commit

Permalink
feat: Be able to specify user_id when creating keypair
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Feb 28, 2022
1 parent 4e51c3f commit 60bac7c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions openstack/compute_keypair_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package openstack

import "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"

const (
computeKeyPairV2UserIDMicroversion = "2.10"
)

// ComputeKeyPairV2CreateOpts is a custom KeyPair struct to include the ValueSpecs field.
type ComputeKeyPairV2CreateOpts struct {
keypairs.CreateOpts
Expand Down
43 changes: 41 additions & 2 deletions openstack/resource_openstack_compute_keypair_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func resourceComputeKeypairV2() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"user_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -67,11 +73,17 @@ func resourceComputeKeypairV2Create(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}

userID := d.Get("user_id").(string)
if userID != "" {
computeClient.Microversion = computeKeyPairV2UserIDMicroversion
}

name := d.Get("name").(string)
createOpts := ComputeKeyPairV2CreateOpts{
keypairs.CreateOpts{
Name: name,
PublicKey: d.Get("public_key").(string),
UserID: d.Get("user_id").(string),
},
MapValueSpecs(d),
}
Expand All @@ -84,6 +96,7 @@ func resourceComputeKeypairV2Create(ctx context.Context, d *schema.ResourceData,
}

d.SetId(kp.Name)
d.Set("user_id", d.Get("user_id").(string))

// Private Key is only available in the response to a create.
d.Set("private_key", kp.PrivateKey)
Expand All @@ -98,7 +111,18 @@ func resourceComputeKeypairV2Read(_ context.Context, d *schema.ResourceData, met
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}

kp, err := keypairs.Get(computeClient, d.Id(), keypairs.GetOpts{}).Extract()
userID := d.Get("user_id").(string)
if userID != "" {
computeClient.Microversion = computeKeyPairV2UserIDMicroversion
}

log.Printf("[DEBUG] Microversion %s", computeClient.Microversion)

kpopts := keypairs.GetOpts{
UserID: userID,
}

kp, err := keypairs.Get(computeClient, d.Id(), kpopts).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error retrieving openstack_compute_keypair_v2"))
}
Expand All @@ -109,6 +133,9 @@ func resourceComputeKeypairV2Read(_ context.Context, d *schema.ResourceData, met
d.Set("public_key", kp.PublicKey)
d.Set("fingerprint", kp.Fingerprint)
d.Set("region", GetRegion(d, config))
if userID != "" {
d.Set("user_id", kp.UserID)
}

return nil
}
Expand All @@ -120,7 +147,19 @@ func resourceComputeKeypairV2Delete(_ context.Context, d *schema.ResourceData, m
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}

err = keypairs.Delete(computeClient, d.Id(), keypairs.DeleteOpts{}).ExtractErr()
userID := d.Get("user_id").(string)
if userID != "" {
computeClient.Microversion = computeKeyPairV2UserIDMicroversion
}

log.Printf("[DEBUG] User ID %s", userID)
log.Printf("[DEBUG] Microversion %s", computeClient.Microversion)

kpopts := keypairs.DeleteOpts{
UserID: userID,
}

err = keypairs.Delete(computeClient, d.Id(), kpopts).ExtractErr()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error deleting openstack_compute_keypair_v2"))
}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/compute_keypair_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ The following arguments are supported:
created, then destroying this resource means you will lose access to that
keypair forever.

* `user_id` - (Optional) This allows administrative users to operate key-pairs
of specified user ID. For this feature your need to have openstack microversion
2.10 (Liberty) or later.

* `value_specs` - (Optional) Map of additional options.

## Attributes Reference
Expand Down

0 comments on commit 60bac7c

Please sign in to comment.