github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/services/requests.go (about) 1 package services 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 // CreateOptsBuilder allows extensions to add additional parameters to 9 // the Create request. 10 type CreateOptsBuilder interface { 11 ToServiceCreateMap() (map[string]interface{}, error) 12 } 13 14 // CreateOpts provides options used to create a service. 15 type CreateOpts struct { 16 // Type is the type of the service. 17 Type string `json:"type"` 18 19 // Enabled is whether or not the service is enabled. 20 Enabled *bool `json:"enabled,omitempty"` 21 22 // Extra is free-form extra key/value pairs to describe the service. 23 Extra map[string]interface{} `json:"-"` 24 } 25 26 // ToServiceCreateMap formats a CreateOpts into a create request. 27 func (opts CreateOpts) ToServiceCreateMap() (map[string]interface{}, error) { 28 b, err := golangsdk.BuildRequestBody(opts, "service") 29 if err != nil { 30 return nil, err 31 } 32 33 if opts.Extra != nil { 34 if v, ok := b["service"].(map[string]interface{}); ok { 35 for key, value := range opts.Extra { 36 v[key] = value 37 } 38 } 39 } 40 41 return b, nil 42 } 43 44 // Create adds a new service of the requested type to the catalog. 45 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 46 b, err := opts.ToServiceCreateMap() 47 if err != nil { 48 r.Err = err 49 return 50 } 51 _, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{ 52 OkCodes: []int{201}, 53 }) 54 return 55 } 56 57 // ListOptsBuilder enables extensions to add additional parameters to the List 58 // request. 59 type ListOptsBuilder interface { 60 ToServiceListMap() (string, error) 61 } 62 63 // ListOpts provides options for filtering the List results. 64 type ListOpts struct { 65 // ServiceType filter the response by a type of service. 66 ServiceType string `q:"type"` 67 68 // Name filters the response by a service name. 69 Name string `q:"name"` 70 } 71 72 // ToServiceListMap builds a list query from the list options. 73 func (opts ListOpts) ToServiceListMap() (string, error) { 74 q, err := golangsdk.BuildQueryString(opts) 75 if err != nil { 76 return "", err 77 } 78 return q.String(), err 79 } 80 81 // List enumerates the services available to a specific user. 82 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 83 url := listURL(client) 84 if opts != nil { 85 query, err := opts.ToServiceListMap() 86 if err != nil { 87 return pagination.Pager{Err: err} 88 } 89 url += query 90 } 91 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 92 return ServicePage{pagination.LinkedPageBase{PageResult: r}} 93 }) 94 } 95 96 // Get returns additional information about a service, given its ID. 97 func Get(client *golangsdk.ServiceClient, serviceID string) (r GetResult) { 98 _, r.Err = client.Get(serviceURL(client, serviceID), &r.Body, nil) 99 return 100 } 101 102 // UpdateOptsBuilder allows extensions to add additional parameters to 103 // the Update request. 104 type UpdateOptsBuilder interface { 105 ToServiceUpdateMap() (map[string]interface{}, error) 106 } 107 108 // UpdateOpts provides options for updating a service. 109 type UpdateOpts struct { 110 // Type is the type of the service. 111 Type string `json:"type"` 112 113 // Enabled is whether or not the service is enabled. 114 Enabled *bool `json:"enabled,omitempty"` 115 116 // Extra is free-form extra key/value pairs to describe the service. 117 Extra map[string]interface{} `json:"-"` 118 } 119 120 // ToServiceUpdateMap formats a UpdateOpts into an update request. 121 func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) { 122 b, err := golangsdk.BuildRequestBody(opts, "service") 123 if err != nil { 124 return nil, err 125 } 126 127 if opts.Extra != nil { 128 if v, ok := b["service"].(map[string]interface{}); ok { 129 for key, value := range opts.Extra { 130 v[key] = value 131 } 132 } 133 } 134 135 return b, nil 136 } 137 138 // Update updates an existing Service. 139 func Update(client *golangsdk.ServiceClient, serviceID string, opts UpdateOptsBuilder) (r UpdateResult) { 140 b, err := opts.ToServiceUpdateMap() 141 if err != nil { 142 r.Err = err 143 return 144 } 145 _, r.Err = client.Patch(updateURL(client, serviceID), &b, &r.Body, &golangsdk.RequestOpts{ 146 OkCodes: []int{200}, 147 }) 148 return 149 } 150 151 // Delete removes an existing service. 152 // It either deletes all associated endpoints, or fails until all endpoints 153 // are deleted. 154 func Delete(client *golangsdk.ServiceClient, serviceID string) (r DeleteResult) { 155 _, r.Err = client.Delete(serviceURL(client, serviceID), nil) 156 return 157 }