Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 4 | "fmt" |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 5 | "net/url" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 6 | |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 8 | tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 9 | endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints" |
| 10 | services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 11 | tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 16 | const ( |
| 17 | v20 = "v2.0" |
| 18 | v30 = "v3.0" |
| 19 | ) |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 20 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 21 | // NewClient prepares an unauthenticated ProviderClient instance. |
| 22 | // Most users will probably prefer using the AuthenticatedClient function instead. |
| 23 | // This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly, |
| 24 | // for example. |
| 25 | func NewClient(endpoint string) (*gophercloud.ProviderClient, error) { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 26 | u, err := url.Parse(endpoint) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | hadPath := u.Path != "" |
| 31 | u.Path, u.RawQuery, u.Fragment = "", "", "" |
| 32 | base := u.String() |
| 33 | |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame^] | 34 | endpoint = gophercloud.NormalizeURL(endpoint) |
| 35 | base = gophercloud.NormalizeURL(base) |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 36 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 37 | if hadPath { |
| 38 | return &gophercloud.ProviderClient{ |
| 39 | IdentityBase: base, |
| 40 | IdentityEndpoint: endpoint, |
| 41 | }, nil |
| 42 | } |
| 43 | |
| 44 | return &gophercloud.ProviderClient{ |
| 45 | IdentityBase: base, |
| 46 | IdentityEndpoint: "", |
| 47 | }, nil |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // AuthenticatedClient logs in to an OpenStack cloud found at the identity endpoint specified by options, acquires a token, and |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 51 | // returns a Client instance that's ready to operate. |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 52 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 53 | // the most recent identity service available to proceed. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 54 | func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) { |
| 55 | client, err := NewClient(options.IdentityEndpoint) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | err = Authenticate(client, options) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | return client, nil |
| 65 | } |
| 66 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 67 | // Authenticate or re-authenticate against the most recent identity service supported at the provided endpoint. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 68 | func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 69 | versions := []*utils.Version{ |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 70 | &utils.Version{ID: v20, Priority: 20, Suffix: "/v2.0/"}, |
| 71 | &utils.Version{ID: v30, Priority: 30, Suffix: "/v3/"}, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 74 | chosen, endpoint, err := utils.ChooseVersion(client.IdentityBase, client.IdentityEndpoint, versions) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 75 | if err != nil { |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 76 | return err |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | switch chosen.ID { |
| 80 | case v20: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 81 | return v2auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 82 | case v30: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 83 | return v3auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 84 | default: |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 85 | // The switch statement must be out of date from the versions list. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 86 | return fmt.Errorf("Unrecognized identity version: %s", chosen.ID) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 90 | // AuthenticateV2 explicitly authenticates against the identity v2 endpoint. |
| 91 | func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 92 | return v2auth(client, "", options) |
| 93 | } |
| 94 | |
| 95 | func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 96 | v2Client := NewIdentityV2(client) |
| 97 | if endpoint != "" { |
| 98 | v2Client.Endpoint = endpoint |
| 99 | } |
| 100 | |
Ash Wilson | 6cc0070 | 2014-10-03 14:00:26 -0400 | [diff] [blame] | 101 | result := tokens2.Create(v2Client, options) |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 102 | |
| 103 | token, err := result.ExtractToken() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 108 | catalog, err := result.ExtractServiceCatalog() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | |
| 113 | client.TokenID = token.ID |
| 114 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
Ash Wilson | 52fbd18 | 2014-10-03 13:48:06 -0400 | [diff] [blame] | 115 | return v2endpointLocator(catalog, opts) |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return nil |
| 119 | } |
| 120 | |
Ash Wilson | 6cc0070 | 2014-10-03 14:00:26 -0400 | [diff] [blame] | 121 | func v2endpointLocator(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) { |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 122 | // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided. |
Ash Wilson | 6cc0070 | 2014-10-03 14:00:26 -0400 | [diff] [blame] | 123 | var endpoints = make([]tokens2.Endpoint, 0, 1) |
| 124 | for _, entry := range catalog.Entries { |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 125 | if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) { |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 126 | for _, endpoint := range entry.Endpoints { |
| 127 | if opts.Region == "" || endpoint.Region == opts.Region { |
| 128 | endpoints = append(endpoints, endpoint) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Report an error if the options were ambiguous. |
| 135 | if len(endpoints) == 0 { |
| 136 | return "", gophercloud.ErrEndpointNotFound |
| 137 | } |
| 138 | if len(endpoints) > 1 { |
| 139 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 140 | } |
| 141 | |
| 142 | // Extract the appropriate URL from the matching Endpoint. |
| 143 | for _, endpoint := range endpoints { |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 144 | switch opts.Availability { |
| 145 | case gophercloud.AvailabilityPublic: |
Ash Wilson | aca58d8 | 2014-09-10 17:00:35 -0400 | [diff] [blame] | 146 | return normalizeURL(endpoint.PublicURL), nil |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 147 | case gophercloud.AvailabilityInternal: |
Ash Wilson | aca58d8 | 2014-09-10 17:00:35 -0400 | [diff] [blame] | 148 | return normalizeURL(endpoint.InternalURL), nil |
Ash Wilson | 6cc0070 | 2014-10-03 14:00:26 -0400 | [diff] [blame] | 149 | case gophercloud.AvailabilityAdmin: |
| 150 | return normalizeURL(endpoint.AdminURL), nil |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 151 | default: |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 152 | return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability) |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 156 | return "", gophercloud.ErrEndpointNotFound |
| 157 | } |
| 158 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 159 | // AuthenticateV3 explicitly authenticates against the identity v3 service. |
| 160 | func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 161 | return v3auth(client, "", options) |
| 162 | } |
| 163 | |
| 164 | func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 165 | // Override the generated service endpoint with the one returned by the version endpoint. |
| 166 | v3Client := NewIdentityV3(client) |
| 167 | if endpoint != "" { |
| 168 | v3Client.Endpoint = endpoint |
| 169 | } |
| 170 | |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 171 | token, err := tokens3.Create(v3Client, options, nil).Extract() |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 172 | if err != nil { |
| 173 | return err |
| 174 | } |
Ash Wilson | 63b2a29 | 2014-10-02 09:29:06 -0400 | [diff] [blame] | 175 | client.TokenID = token.ID |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 176 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 177 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 178 | return v3endpointLocator(v3Client, opts) |
| 179 | } |
| 180 | |
| 181 | return nil |
| 182 | } |
| 183 | |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 184 | func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) { |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 185 | // Discover the service we're interested in. |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 186 | var services = make([]services3.Service, 0, 1) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 187 | servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type}) |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 188 | err := servicePager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 189 | part, err := services3.ExtractServices(page) |
| 190 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 191 | return false, err |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 192 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 193 | |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 194 | for _, service := range part { |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 195 | if service.Name == opts.Name { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 196 | services = append(services, service) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 197 | } |
| 198 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 199 | |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 200 | return true, nil |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 201 | }) |
| 202 | if err != nil { |
| 203 | return "", err |
| 204 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 205 | |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 206 | if len(services) == 0 { |
| 207 | return "", gophercloud.ErrServiceNotFound |
| 208 | } |
| 209 | if len(services) > 1 { |
| 210 | return "", fmt.Errorf("Discovered %d matching services: %#v", len(services), services) |
| 211 | } |
| 212 | service := services[0] |
| 213 | |
| 214 | // Enumerate the endpoints available for this service. |
| 215 | var endpoints []endpoints3.Endpoint |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 216 | endpointPager := endpoints3.List(v3Client, endpoints3.ListOpts{ |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 217 | Availability: opts.Availability, |
| 218 | ServiceID: service.ID, |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 219 | }) |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 220 | err = endpointPager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 221 | part, err := endpoints3.ExtractEndpoints(page) |
| 222 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 223 | return false, err |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | for _, endpoint := range part { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 227 | if opts.Region == "" || endpoint.Region == opts.Region { |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 228 | endpoints = append(endpoints, endpoint) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 229 | } |
| 230 | } |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 231 | |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 232 | return true, nil |
Ash Wilson | 566613e | 2014-09-12 14:51:46 -0400 | [diff] [blame] | 233 | }) |
| 234 | if err != nil { |
| 235 | return "", err |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if len(endpoints) == 0 { |
| 239 | return "", gophercloud.ErrEndpointNotFound |
| 240 | } |
| 241 | if len(endpoints) > 1 { |
| 242 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 243 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 244 | endpoint := endpoints[0] |
| 245 | |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame^] | 246 | return gophercloud.NormalizeURL(endpoint.URL), nil |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 249 | // NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service. |
| 250 | func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 251 | v2Endpoint := client.IdentityBase + "v2.0/" |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 252 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 253 | return &gophercloud.ServiceClient{ |
| 254 | Provider: client, |
| 255 | Endpoint: v2Endpoint, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 256 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 259 | // NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service. |
| 260 | func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 261 | v3Endpoint := client.IdentityBase + "v3/" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 262 | |
| 263 | return &gophercloud.ServiceClient{ |
| 264 | Provider: client, |
| 265 | Endpoint: v3Endpoint, |
| 266 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 267 | } |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 268 | |
| 269 | // NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package. |
Jon Perritt | 509fbb6 | 2014-09-10 13:29:56 -0500 | [diff] [blame] | 270 | func NewStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 271 | eo.ApplyDefaults("object-store") |
| 272 | url, err := client.EndpointLocator(eo) |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 273 | if err != nil { |
| 274 | return nil, err |
| 275 | } |
| 276 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 277 | } |
Ash Wilson | 5e57c1b | 2014-09-17 09:24:46 -0400 | [diff] [blame] | 278 | |
| 279 | // NewComputeV2 creates a ServiceClient that may be used with the v2 compute package. |
| 280 | func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 281 | eo.ApplyDefaults("compute") |
| 282 | url, err := client.EndpointLocator(eo) |
| 283 | if err != nil { |
| 284 | return nil, err |
| 285 | } |
| 286 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 287 | } |
Ash Wilson | ebc3d12 | 2014-09-24 13:44:05 -0400 | [diff] [blame] | 288 | |
| 289 | // NewNetworkV2 creates a ServiceClient that may be used with the v2 network package. |
Jamie Hannaford | 7ea2958 | 2014-09-11 15:49:46 +0200 | [diff] [blame] | 290 | func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 291 | eo.ApplyDefaults("network") |
| 292 | url, err := client.EndpointLocator(eo) |
| 293 | if err != nil { |
| 294 | return nil, err |
| 295 | } |
Ash Wilson | 99541ab | 2014-10-06 17:32:39 -0400 | [diff] [blame] | 296 | return &gophercloud.ServiceClient{ |
| 297 | Provider: client, |
| 298 | Endpoint: url, |
| 299 | ResourceBase: url + "v2.0/", |
| 300 | }, nil |
Jamie Hannaford | 7ea2958 | 2014-09-11 15:49:46 +0200 | [diff] [blame] | 301 | } |
Jon Perritt | c5ee85e | 2014-09-17 00:53:19 -0500 | [diff] [blame] | 302 | |
| 303 | // NewBlockStorageV1 creates a ServiceClient that may be used to access the v1 block storage service. |
| 304 | func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 305 | eo.ApplyDefaults("volume") |
| 306 | url, err := client.EndpointLocator(eo) |
| 307 | if err != nil { |
| 308 | return nil, err |
| 309 | } |
| 310 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 311 | } |