| Ash Wilson | 3d89f20 | 2014-10-31 10:23:29 -0400 | [diff] [blame^] | 1 | /* | 
 | 2 | Package gophercloud provides a multi-vendor interface to OpenStack-compatible | 
 | 3 | clouds. The library has a three-level hierarchy: providers, services, and | 
 | 4 | resources. | 
 | 5 |  | 
 | 6 | Provider structs represent the service providers that offer and manage a | 
 | 7 | collection of services. Examples of providers include: OpenStack, Rackspace, | 
 | 8 | HP. These are defined like so: | 
 | 9 |  | 
 | 10 |   opts := gophercloud.AuthOptions{ | 
 | 11 |     IdentityEndpoint: "https://my-openstack.com:5000/v2.0", | 
 | 12 |     Username: "{username}", | 
 | 13 |     Password: "{password}", | 
 | 14 |     TenantID: "{tenant_id}", | 
 | 15 |   } | 
 | 16 |  | 
 | 17 |   provider, err := openstack.AuthenticatedClient(opts) | 
 | 18 |  | 
 | 19 | Service structs are specific to a provider and handle all of the logic and | 
 | 20 | operations for a particular OpenStack service. Examples of services include: | 
 | 21 | Compute, Object Storage, Block Storage. In order to define one, you need to | 
 | 22 | pass in the parent provider, like so: | 
 | 23 |  | 
 | 24 |   opts := gophercloud.EndpointOpts{Region: "RegionOne"} | 
 | 25 |  | 
 | 26 |   client := openstack.NewComputeV2(provider, opts) | 
 | 27 |  | 
 | 28 | Resource structs are the domain models that services make use of in order | 
 | 29 | to work with and represent the state of API resources: | 
 | 30 |  | 
 | 31 |   server, err := servers.Get(client, "{serverId}").Extract() | 
 | 32 |  | 
 | 33 | Intermediate Result structs are returned for API operations, which allow | 
 | 34 | generic access to the HTTP headers, response body, and any errors associated | 
 | 35 | with the network transaction. To turn a result into a usable resource struct, | 
 | 36 | you must call the Extract method which is chained to the response, or an | 
 | 37 | Extract function from an applicable extension: | 
 | 38 |  | 
 | 39 |   result := servers.Get(client, "{serverId}") | 
 | 40 |  | 
 | 41 |   // Attempt to extract the disk configuration from the OS-DCF disk config | 
 | 42 |   // extension: | 
 | 43 |   config, err := diskconfig.ExtractGet(result) | 
 | 44 |  | 
 | 45 | All requests that enumerate a collection return a Pager struct that is used to | 
 | 46 | iterate through the results one page at a time. Use the EachPage method on that | 
 | 47 | Pager to handle each successive Page in a closure, then use the appropriate | 
 | 48 | extraction method from that requests' package to interpret that Page as a slice | 
 | 49 | of results: | 
 | 50 |  | 
 | 51 |   err := servers.List(client, nil).EachPage(func (page pagination.Page) (bool, error) { | 
 | 52 |     s, err := servers.ExtractServers(page) | 
 | 53 |     if err != nil { | 
 | 54 |       return false, err | 
 | 55 |     } | 
 | 56 |  | 
 | 57 |     // Handle the []servers.Server slice. | 
 | 58 |  | 
 | 59 |     // Return "false" or an error to prematurely stop fetching new pages. | 
 | 60 |     return true, nil | 
 | 61 |   }) | 
 | 62 |  | 
 | 63 | This top-level package contains utility functions and data types that are used | 
 | 64 | throughout the provider and service packages. Of particular note for end users | 
 | 65 | are the AuthOptions and EndpointOpts structs. | 
 | 66 | */ | 
 | 67 | package gophercloud |