ceph-csi/vendor/github.com/IBM/keyprotect-go-client/key_alias.go
dependabot[bot] 4b709310e2 rebase: bump github.com/IBM/keyprotect-go-client from 0.7.0 to 0.8.0
Bumps [github.com/IBM/keyprotect-go-client](https://github.com/IBM/keyprotect-go-client) from 0.7.0 to 0.8.0.
- [Release notes](https://github.com/IBM/keyprotect-go-client/releases)
- [Commits](https://github.com/IBM/keyprotect-go-client/compare/v0.7.0...v0.8.0)

---
updated-dependencies:
- dependency-name: github.com/IBM/keyprotect-go-client
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-07-12 17:32:46 +00:00

69 lines
1.9 KiB
Go

package kp
import (
"context"
"fmt"
"time"
)
var (
requestPath = "keys/%s/aliases/%s"
)
// KeyAlias represents an Alias details of a key as returned by KP API
type KeyAlias struct {
KeyID string `json:"keyId,omitempty"`
Alias string `json:"alias,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
CreationDate *time.Time `json:"creationDate,omitempty"`
}
// AliasesMetadata represents the metadata of a collection of aliases
type AliasesMetadata struct {
CollectionType string `json:"collectionType"`
NumberOfAliases int `json:"collectionTotal"`
}
type KeyAliases struct {
Metadata AliasesMetadata `json:"metadata"`
KeyAliases []KeyAlias `json:"resources"`
}
// CreateKeyAlias creates an alias name for a key.
// An alias name acts as an identifier just like key ID
// For more information please refer to the link below:
// https://cloud.ibm.com/docs/key-protect?topic=key-protect-create-key-alias#create-key-alias-api
func (c *Client) CreateKeyAlias(ctx context.Context, aliasName, idOrAlias string) (*KeyAlias, error) {
req, err := c.newRequest("POST", fmt.Sprintf(requestPath, idOrAlias, aliasName), nil)
if err != nil {
return nil, err
}
aliasesResponse := KeyAliases{}
_, err = c.do(ctx, req, &aliasesResponse)
if err != nil {
return nil, err
}
if len(aliasesResponse.KeyAliases) == 0 {
return nil, nil
}
return &aliasesResponse.KeyAliases[0], nil
}
// DeleteKeyAlias deletes an alias name associated with a key
// For more information please refer to the link below:
// https://cloud.ibm.com/docs/key-protect?topic=key-protect-create-key-alias#delete-key-alias
func (c *Client) DeleteKeyAlias(ctx context.Context, aliasName, idOrAlias string) error {
req, err := c.newRequest("DELETE", fmt.Sprintf(requestPath, idOrAlias, aliasName), nil)
if err != nil {
return err
}
_, err = c.do(ctx, req, nil)
return err
}