github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/kms/v1/keys/requests.go (about)

     1  package keys
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	//"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type CreateOpts struct {
     9  	// Alias of a CMK
    10  	KeyAlias string `json:"key_alias" required:"true"`
    11  	// CMK description
    12  	KeyDescription string `json:"key_description,omitempty"`
    13  	// Region where a CMK resides
    14  	Realm string `json:"realm,omitempty"`
    15  	// Purpose of a CMK (The default value is Encrypt_Decrypt)
    16  	KeyUsage string `json:"key_usage,omitempty"`
    17  	// Enterprise project id
    18  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
    19  }
    20  
    21  type DeleteOpts struct {
    22  	// ID of a CMK
    23  	KeyID string `json:"key_id" required:"true"`
    24  	// Number of days after which a CMK is scheduled to be deleted
    25  	// (The value ranges from 7 to 1096.)
    26  	PendingDays string `json:"pending_days" required:"true"`
    27  }
    28  
    29  type UpdateAliasOpts struct {
    30  	// ID of a CMK
    31  	KeyID string `json:"key_id" required:"true"`
    32  	// CMK description
    33  	KeyAlias string `json:"key_alias" required:"true"`
    34  }
    35  
    36  type UpdateDesOpts struct {
    37  	// ID of a CMK
    38  	KeyID string `json:"key_id" required:"true"`
    39  	// CMK description
    40  	KeyDescription string `json:"key_description" required:"true"`
    41  }
    42  
    43  type DataEncryptOpts struct {
    44  	// ID of a CMK
    45  	KeyID string `json:"key_id" required:"true"`
    46  	// CMK description
    47  	EncryptionContext string `json:"encryption_context,omitempty"`
    48  	// 36-byte serial number of a request message
    49  	DatakeyLength string `json:"datakey_length,omitempty"`
    50  }
    51  
    52  type EncryptDEKOpts struct {
    53  	// ID of a CMK
    54  	KeyID string `json:"key_id" required:"true"`
    55  	// CMK description
    56  	EncryptionContext string `json:"encryption_context,omitempty"`
    57  	// 36-byte serial number of a request message
    58  	DataKeyPlainLength string `json:"datakey_plain_length,omitempty"`
    59  	// Both the plaintext (64 bytes) of a DEK and the SHA-256 hash value (32 bytes)
    60  	// of the plaintext are expressed as a hexadecimal character string.
    61  	PlainText string `json:"plain_text" required:"true"`
    62  }
    63  
    64  // ListOpts holds options for listing Volumes. It is passed to the volumes.List
    65  // function.
    66  type ListOpts struct {
    67  	// State of a CMK
    68  	KeyState            string `json:"key_state,omitempty"`
    69  	Limit               string `json:"limit,omitempty"`
    70  	Marker              string `json:"marker,omitempty"`
    71  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
    72  }
    73  
    74  // ToKeyCreateMap assembles a request body based on the contents of a
    75  // CreateOpts.
    76  func (opts CreateOpts) ToKeyCreateMap() (map[string]interface{}, error) {
    77  	return golangsdk.BuildRequestBody(opts, "")
    78  }
    79  
    80  // ToKeyDeleteMap assembles a request body based on the contents of a
    81  // DeleteOpts.
    82  func (opts DeleteOpts) ToKeyDeleteMap() (map[string]interface{}, error) {
    83  	return golangsdk.BuildRequestBody(opts, "")
    84  }
    85  
    86  // ToKeyUpdateAliasMap assembles a request body based on the contents of a
    87  // UpdateAliasOpts.
    88  func (opts UpdateAliasOpts) ToKeyUpdateAliasMap() (map[string]interface{}, error) {
    89  	return golangsdk.BuildRequestBody(opts, "")
    90  }
    91  
    92  // ToKeyUpdateDesMap assembles a request body based on the contents of a
    93  // UpdateDesOpts.
    94  func (opts UpdateDesOpts) ToKeyUpdateDesMap() (map[string]interface{}, error) {
    95  	return golangsdk.BuildRequestBody(opts, "")
    96  }
    97  
    98  func (opts DataEncryptOpts) ToDataEncryptMap() (map[string]interface{}, error) {
    99  	return golangsdk.BuildRequestBody(opts, "")
   100  }
   101  
   102  func (opts EncryptDEKOpts) ToEncryptDEKMap() (map[string]interface{}, error) {
   103  	return golangsdk.BuildRequestBody(opts, "")
   104  }
   105  
   106  func (opts ListOpts) ToKeyListMap() (map[string]interface{}, error) {
   107  	return golangsdk.BuildRequestBody(opts, "")
   108  }
   109  
   110  type CreateOptsBuilder interface {
   111  	ToKeyCreateMap() (map[string]interface{}, error)
   112  }
   113  
   114  type DeleteOptsBuilder interface {
   115  	ToKeyDeleteMap() (map[string]interface{}, error)
   116  }
   117  
   118  type UpdateAliasOptsBuilder interface {
   119  	ToKeyUpdateAliasMap() (map[string]interface{}, error)
   120  }
   121  
   122  type UpdateDesOptsBuilder interface {
   123  	ToKeyUpdateDesMap() (map[string]interface{}, error)
   124  }
   125  
   126  type DataEncryptOptsBuilder interface {
   127  	ToDataEncryptMap() (map[string]interface{}, error)
   128  }
   129  
   130  type EncryptDEKOptsBuilder interface {
   131  	ToEncryptDEKMap() (map[string]interface{}, error)
   132  }
   133  
   134  type ListOptsBuilder interface {
   135  	ToKeyListMap() (map[string]interface{}, error)
   136  }
   137  
   138  // Create will create a new key based on the values in CreateOpts. To ExtractKeyInfo
   139  // the key object from the response, call the ExtractKeyInfo method on the
   140  // CreateResult.
   141  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   142  	b, err := opts.ToKeyCreateMap()
   143  	if err != nil {
   144  		r.Err = err
   145  		return
   146  	}
   147  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   148  		OkCodes: []int{200},
   149  	})
   150  	return
   151  }
   152  
   153  // Get retrieves the key with the provided ID. To extract the key object
   154  // from the response, call the Extract method on the GetResult.
   155  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   156  	b := map[string]interface{}{"key_id": id}
   157  	_, r.Err = client.Post(getURL(client), &b, &r.Body, &golangsdk.RequestOpts{
   158  		OkCodes: []int{200},
   159  	})
   160  	return
   161  }
   162  
   163  // Delete will delete the existing key with the provided ID.
   164  func Delete(client *golangsdk.ServiceClient, opts DeleteOptsBuilder) (r DeleteResult) {
   165  	b, err := opts.ToKeyDeleteMap()
   166  	if err != nil {
   167  		r.Err = err
   168  		return
   169  	}
   170  	_, r.Err = client.Post(deleteURL(client), b, &r.Body, &golangsdk.RequestOpts{
   171  		OkCodes:      []int{200},
   172  		JSONResponse: &r.Body,
   173  	})
   174  	return
   175  }
   176  
   177  func UpdateAlias(client *golangsdk.ServiceClient, opts UpdateAliasOptsBuilder) (r UpdateAliasResult) {
   178  	b, err := opts.ToKeyUpdateAliasMap()
   179  	if err != nil {
   180  		r.Err = err
   181  		return
   182  	}
   183  	_, r.Err = client.Post(updateAliasURL(client), b, &r.Body, &golangsdk.RequestOpts{
   184  		OkCodes: []int{200},
   185  	})
   186  	return
   187  }
   188  
   189  func UpdateDes(client *golangsdk.ServiceClient, opts UpdateDesOptsBuilder) (r UpdateDesResult) {
   190  	b, err := opts.ToKeyUpdateDesMap()
   191  	if err != nil {
   192  		r.Err = err
   193  		return
   194  	}
   195  	_, r.Err = client.Post(updateDesURL(client), b, &r.Body, &golangsdk.RequestOpts{
   196  		OkCodes: []int{200},
   197  	})
   198  	return
   199  }
   200  
   201  func DataEncryptGet(client *golangsdk.ServiceClient, opts DataEncryptOptsBuilder) (r DataEncryptResult) {
   202  	b, err := opts.ToDataEncryptMap()
   203  	if err != nil {
   204  		r.Err = err
   205  		return
   206  	}
   207  	_, r.Err = client.Post(dataEncryptURL(client), b, &r.Body, &golangsdk.RequestOpts{
   208  		OkCodes: []int{200},
   209  	})
   210  	return
   211  }
   212  
   213  func DataEncryptGetWithoutPlaintext(client *golangsdk.ServiceClient, opts DataEncryptOptsBuilder) (r DataEncryptResult) {
   214  	b, err := opts.ToDataEncryptMap()
   215  	if err != nil {
   216  		r.Err = err
   217  		return
   218  	}
   219  	_, r.Err = client.Post(dataEncryptURL(client), b, &r.Body, &golangsdk.RequestOpts{
   220  		OkCodes: []int{200},
   221  	})
   222  	return
   223  }
   224  
   225  func EncryptDEKGet(client *golangsdk.ServiceClient, opts EncryptDEKOptsBuilder) (r EncryptDEKResult) {
   226  	b, err := opts.ToEncryptDEKMap()
   227  	if err != nil {
   228  		r.Err = err
   229  		return
   230  	}
   231  	_, r.Err = client.Post(encryptDEKURL(client), b, &r.Body, &golangsdk.RequestOpts{
   232  		OkCodes: []int{200},
   233  	})
   234  	return
   235  }
   236  
   237  func EnableKey(client *golangsdk.ServiceClient, id string) (r ExtractUpdateKeyStateResult) {
   238  	b := map[string]interface{}{"key_id": id}
   239  	_, r.Err = client.Post(enableKeyURL(client), b, &r.Body, &golangsdk.RequestOpts{
   240  		OkCodes: []int{200},
   241  	})
   242  	return
   243  }
   244  
   245  func DisableKey(client *golangsdk.ServiceClient, id string) (r ExtractUpdateKeyStateResult) {
   246  	b := map[string]interface{}{"key_id": id}
   247  	_, r.Err = client.Post(disableKeyURL(client), b, &r.Body, &golangsdk.RequestOpts{
   248  		OkCodes: []int{200},
   249  	})
   250  	return
   251  }
   252  
   253  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) (r ListResult) {
   254  	b, err := opts.ToKeyListMap()
   255  	if err != nil {
   256  		r.Err = err
   257  		return
   258  	}
   259  	_, r.Err = client.Post(listURL(client), b, &r.Body, &golangsdk.RequestOpts{
   260  		OkCodes: []int{200},
   261  	})
   262  	return
   263  }
   264  
   265  func ListAllKeys(client *golangsdk.ServiceClient, opts ListOptsBuilder) (r ListResult) {
   266  	b, err := opts.ToKeyListMap()
   267  	if err != nil {
   268  		r.Err = err
   269  		return
   270  	}
   271  	_, r.Err = client.Post(listURL(client), b, &r.Body, &golangsdk.RequestOpts{
   272  		OkCodes: []int{200},
   273  	})
   274  	return
   275  }
   276  
   277  //func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) (r GetResult) {
   278  //	//url := listURL(client)
   279  //	//if opts != nil {
   280  //	//	query, err := opts.ToKeyListQuery()
   281  //	//	if err != nil {
   282  //	//		return pagination.Pager{Err: err}
   283  //	//	}
   284  //	//	url += query
   285  //	//}
   286  //	b, err := opts.ToKeyListQuery()
   287  //	if err != nil {
   288  //		r.Err = err
   289  //		return
   290  //	}
   291  //	_, r.Err = client.Post(listURL(client), b, &r.Body, &golangsdk.RequestOpts{
   292  //		OkCodes: []int{200},
   293  //	})
   294  //	return
   295  //
   296  //	//return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   297  //	//	return KeyPage{pagination.LinkedPageBase{PageResult: r}}
   298  //	//})
   299  //}