|
| 1 | +package tfe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +var _ HYOKCustomerKeyVersions = (*hyokCustomerKeyVersions)(nil) |
| 11 | + |
| 12 | +// HYOKCustomerKeyVersions describes all the hyok customer key version related methods that the HCP Terraform API supports. |
| 13 | +// HCP Terraform API docs: |
| 14 | +// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/hold-your-own-key/key-versions |
| 15 | +type HYOKCustomerKeyVersions interface { |
| 16 | + // List all hyok customer key versions associated to a HYOK configuration. |
| 17 | + List(ctx context.Context, hyokConfigurationID string, options *HYOKCustomerKeyVersionListOptions) (*HYOKCustomerKeyVersionList, error) |
| 18 | + |
| 19 | + // Read a hyok customer key version by its ID. |
| 20 | + Read(ctx context.Context, hyokCustomerKeyVersionID string) (*HYOKCustomerKeyVersion, error) |
| 21 | + |
| 22 | + // Revoke a hyok customer key version. |
| 23 | + Revoke(ctx context.Context, hyokCustomerKeyVersionID string) error |
| 24 | + |
| 25 | + // Delete a hyok customer key version. |
| 26 | + Delete(ctx context.Context, hyokCustomerKeyVersionID string) error |
| 27 | +} |
| 28 | + |
| 29 | +// hyokCustomerKeyVersions implements HYOKCustomerKeyVersions |
| 30 | +type hyokCustomerKeyVersions struct { |
| 31 | + client *Client |
| 32 | +} |
| 33 | + |
| 34 | +// HYOKCustomerKeyVersionList represents a list of hyok customer key versions |
| 35 | +type HYOKCustomerKeyVersionList struct { |
| 36 | + *Pagination |
| 37 | + Items []*HYOKCustomerKeyVersion |
| 38 | +} |
| 39 | + |
| 40 | +// HYOKCustomerKeyVersion represents the resource |
| 41 | +type HYOKCustomerKeyVersion struct { |
| 42 | + // Attributes |
| 43 | + ID string `jsonapi:"primary,hyok-customer-key-versions"` |
| 44 | + KeyVersion string `jsonapi:"attr,key-version"` |
| 45 | + CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"` |
| 46 | + UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"` |
| 47 | + RevokedAt time.Time `jsonapi:"attr,revoked-at,iso8601"` |
| 48 | + Status HYOKKeyVersionStatus `jsonapi:"attr,status"` |
| 49 | + Error string `jsonapi:"attr,error"` |
| 50 | + |
| 51 | + // Relationships |
| 52 | + HYOKConfiguration *HYOKConfiguration `jsonapi:"relation,hyok-configuration"` |
| 53 | +} |
| 54 | + |
| 55 | +// HYOKKeyVersionStatus represents a key version status. |
| 56 | +type HYOKKeyVersionStatus string |
| 57 | + |
| 58 | +// List all available configuration version statuses. |
| 59 | +const ( |
| 60 | + KeyVersionStatusAvailable HYOKKeyVersionStatus = "available" |
| 61 | + KeyVersionStatusRevoking HYOKKeyVersionStatus = "revoking" |
| 62 | + KeyVersionStatusRevoked HYOKKeyVersionStatus = "revoked" |
| 63 | + KeyVersionStatusRevocationFailed HYOKKeyVersionStatus = "revocation_failed" |
| 64 | +) |
| 65 | + |
| 66 | +// HYOKCustomerKeyVersionListOptions represents the options for listing hyok customer key versions |
| 67 | +type HYOKCustomerKeyVersionListOptions struct { |
| 68 | + ListOptions |
| 69 | + Refresh bool `url:"refresh,omitempty"` |
| 70 | +} |
| 71 | + |
| 72 | +// List all hyok customer key versions. |
| 73 | +func (s *hyokCustomerKeyVersions) List(ctx context.Context, hyokConfigurationID string, options *HYOKCustomerKeyVersionListOptions) (*HYOKCustomerKeyVersionList, error) { |
| 74 | + if !validStringID(&hyokConfigurationID) { |
| 75 | + return nil, ErrInvalidHYOK |
| 76 | + } |
| 77 | + |
| 78 | + path := fmt.Sprintf("hyok-configurations/%s/hyok-customer-key-versions", url.PathEscape(hyokConfigurationID)) |
| 79 | + req, err := s.client.NewRequest("GET", path, options) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + kvs := &HYOKCustomerKeyVersionList{} |
| 85 | + err = req.Do(ctx, kvs) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + return kvs, nil |
| 91 | +} |
| 92 | + |
| 93 | +// Read a hyok customer key version by its ID. |
| 94 | +func (s *hyokCustomerKeyVersions) Read(ctx context.Context, hyokCustomerKeyVersionID string) (*HYOKCustomerKeyVersion, error) { |
| 95 | + if !validStringID(&hyokCustomerKeyVersionID) { |
| 96 | + return nil, ErrInvalidHYOKCustomerKeyVersion |
| 97 | + } |
| 98 | + |
| 99 | + path := fmt.Sprintf("hyok-customer-key-versions/%s", url.PathEscape(hyokCustomerKeyVersionID)) |
| 100 | + req, err := s.client.NewRequest("GET", path, nil) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + kv := &HYOKCustomerKeyVersion{} |
| 106 | + err = req.Do(ctx, kv) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + return kv, nil |
| 112 | +} |
| 113 | + |
| 114 | +// Revoke a hyok customer key version. This process is asynchronous. |
| 115 | +// Returns `error` if there was a problem triggering the revocation. Otherwise revocation has been triggered successfully. |
| 116 | +func (s *hyokCustomerKeyVersions) Revoke(ctx context.Context, hyokCustomerKeyVersionID string) error { |
| 117 | + if !validStringID(&hyokCustomerKeyVersionID) { |
| 118 | + return ErrInvalidHYOKCustomerKeyVersion |
| 119 | + } |
| 120 | + |
| 121 | + path := fmt.Sprintf("hyok-customer-key-versions/%s/actions/revoke", url.PathEscape(hyokCustomerKeyVersionID)) |
| 122 | + req, err := s.client.NewRequest("POST", path, nil) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + return req.Do(ctx, nil) |
| 128 | +} |
| 129 | + |
| 130 | +// Delete a hyok customer key version. |
| 131 | +func (s *hyokCustomerKeyVersions) Delete(ctx context.Context, hyokCustomerKeyVersionID string) error { |
| 132 | + if !validStringID(&hyokCustomerKeyVersionID) { |
| 133 | + return ErrInvalidHYOKCustomerKeyVersion |
| 134 | + } |
| 135 | + |
| 136 | + path := fmt.Sprintf("hyok-customer-key-versions/%s", url.PathEscape(hyokCustomerKeyVersionID)) |
| 137 | + req, err := s.client.NewRequest("DELETE", path, nil) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + return req.Do(ctx, nil) |
| 143 | +} |
0 commit comments