github.com/gophercloud/gophercloud@v1.11.0/openstack/keymanager/v1/acls/requests.go (about)

     1  package acls
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  )
     6  
     7  // GetContainerACL retrieves the ACL of a container.
     8  func GetContainerACL(client *gophercloud.ServiceClient, containerID string) (r ACLResult) {
     9  	resp, err := client.Get(containerURL(client, containerID), &r.Body, nil)
    10  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    11  	return
    12  }
    13  
    14  // GetSecretACL retrieves the ACL of a secret.
    15  func GetSecretACL(client *gophercloud.ServiceClient, secretID string) (r ACLResult) {
    16  	resp, err := client.Get(secretURL(client, secretID), &r.Body, nil)
    17  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    18  	return
    19  }
    20  
    21  // SetOptsBuilder allows extensions to add additional parameters to the
    22  // Set request.
    23  type SetOptsBuilder interface {
    24  	ToACLSetMap() (map[string]interface{}, error)
    25  }
    26  
    27  // SetOpt represents options to set a particular ACL type on a resource.
    28  type SetOpt struct {
    29  	// Type is the type of ACL to set. ie: read.
    30  	Type string `json:"-" required:"true"`
    31  
    32  	// Users are the list of Keystone user UUIDs.
    33  	Users *[]string `json:"users,omitempty"`
    34  
    35  	// ProjectAccess toggles if all users in a project can access the resource.
    36  	ProjectAccess *bool `json:"project-access,omitempty"`
    37  }
    38  
    39  // SetOpts represents options to set an ACL on a resource.
    40  type SetOpts []SetOpt
    41  
    42  // ToACLSetMap formats a SetOpts into a set request.
    43  func (opts SetOpts) ToACLSetMap() (map[string]interface{}, error) {
    44  	b := make(map[string]interface{})
    45  	for _, v := range opts {
    46  		m, err := gophercloud.BuildRequestBody(v, v.Type)
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  		b[v.Type] = m[v.Type]
    51  	}
    52  	return b, nil
    53  }
    54  
    55  // SetContainerACL will set an ACL on a container.
    56  func SetContainerACL(client *gophercloud.ServiceClient, containerID string, opts SetOptsBuilder) (r ACLRefResult) {
    57  	b, err := opts.ToACLSetMap()
    58  	if err != nil {
    59  		r.Err = err
    60  		return
    61  	}
    62  
    63  	resp, err := client.Put(containerURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{
    64  		OkCodes: []int{200},
    65  	})
    66  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    67  	return
    68  }
    69  
    70  // SetSecretACL will set an ACL on a secret.
    71  func SetSecretACL(client *gophercloud.ServiceClient, secretID string, opts SetOptsBuilder) (r ACLRefResult) {
    72  	b, err := opts.ToACLSetMap()
    73  	if err != nil {
    74  		r.Err = err
    75  		return
    76  	}
    77  
    78  	resp, err := client.Put(secretURL(client, secretID), &b, &r.Body, &gophercloud.RequestOpts{
    79  		OkCodes: []int{200},
    80  	})
    81  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    82  	return
    83  }
    84  
    85  // UpdateContainerACL will update an ACL on a container.
    86  func UpdateContainerACL(client *gophercloud.ServiceClient, containerID string, opts SetOptsBuilder) (r ACLRefResult) {
    87  	b, err := opts.ToACLSetMap()
    88  	if err != nil {
    89  		r.Err = err
    90  		return
    91  	}
    92  
    93  	resp, err := client.Patch(containerURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{
    94  		OkCodes: []int{200},
    95  	})
    96  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    97  	return
    98  }
    99  
   100  // UpdateSecretACL will update an ACL on a secret.
   101  func UpdateSecretACL(client *gophercloud.ServiceClient, secretID string, opts SetOptsBuilder) (r ACLRefResult) {
   102  	b, err := opts.ToACLSetMap()
   103  	if err != nil {
   104  		r.Err = err
   105  		return
   106  	}
   107  
   108  	resp, err := client.Patch(secretURL(client, secretID), &b, &r.Body, &gophercloud.RequestOpts{
   109  		OkCodes: []int{200},
   110  	})
   111  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   112  	return
   113  }
   114  
   115  // DeleteContainerACL will delete an ACL from a conatiner.
   116  func DeleteContainerACL(client *gophercloud.ServiceClient, containerID string) (r DeleteResult) {
   117  	resp, err := client.Delete(containerURL(client, containerID), &gophercloud.RequestOpts{
   118  		OkCodes: []int{200},
   119  	})
   120  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   121  	return
   122  }
   123  
   124  // DeleteSecretACL will delete an ACL from a secret.
   125  func DeleteSecretACL(client *gophercloud.ServiceClient, secretID string) (r DeleteResult) {
   126  	resp, err := client.Delete(secretURL(client, secretID), &gophercloud.RequestOpts{
   127  		OkCodes: []int{200},
   128  	})
   129  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   130  	return
   131  }