doc.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Authenticating with Providers
  6. Provider structs represent the cloud providers that offer and manage a
  7. collection of services. You will generally want to create one Provider
  8. client per OpenStack cloud.
  9. Use your OpenStack credentials to create a Provider client. The
  10. IdentityEndpoint is typically refered to as "auth_url" or "OS_AUTH_URL" in
  11. information provided by the cloud operator. Additionally, the cloud may refer to
  12. TenantID or TenantName as project_id and project_name. Credentials are
  13. specified like so:
  14. opts := gophercloud.AuthOptions{
  15. IdentityEndpoint: "https://openstack.example.com:5000/v2.0",
  16. Username: "{username}",
  17. Password: "{password}",
  18. TenantID: "{tenant_id}",
  19. }
  20. provider, err := openstack.AuthenticatedClient(opts)
  21. You may also use the openstack.AuthOptionsFromEnv() helper function. This
  22. function reads in standard environment variables frequently found in an
  23. OpenStack `openrc` file. Again note that Gophercloud currently uses "tenant"
  24. instead of "project".
  25. opts, err := openstack.AuthOptionsFromEnv()
  26. provider, err := openstack.AuthenticatedClient(opts)
  27. Service Clients
  28. Service structs are specific to a provider and handle all of the logic and
  29. operations for a particular OpenStack service. Examples of services include:
  30. Compute, Object Storage, Block Storage. In order to define one, you need to
  31. pass in the parent provider, like so:
  32. opts := gophercloud.EndpointOpts{Region: "RegionOne"}
  33. client := openstack.NewComputeV2(provider, opts)
  34. Resources
  35. Resource structs are the domain models that services make use of in order
  36. to work with and represent the state of API resources:
  37. server, err := servers.Get(client, "{serverId}").Extract()
  38. Intermediate Result structs are returned for API operations, which allow
  39. generic access to the HTTP headers, response body, and any errors associated
  40. with the network transaction. To turn a result into a usable resource struct,
  41. you must call the Extract method which is chained to the response, or an
  42. Extract function from an applicable extension:
  43. result := servers.Get(client, "{serverId}")
  44. // Attempt to extract the disk configuration from the OS-DCF disk config
  45. // extension:
  46. config, err := diskconfig.ExtractGet(result)
  47. All requests that enumerate a collection return a Pager struct that is used to
  48. iterate through the results one page at a time. Use the EachPage method on that
  49. Pager to handle each successive Page in a closure, then use the appropriate
  50. extraction method from that request's package to interpret that Page as a slice
  51. of results:
  52. err := servers.List(client, nil).EachPage(func (page pagination.Page) (bool, error) {
  53. s, err := servers.ExtractServers(page)
  54. if err != nil {
  55. return false, err
  56. }
  57. // Handle the []servers.Server slice.
  58. // Return "false" or an error to prematurely stop fetching new pages.
  59. return true, nil
  60. })
  61. If you want to obtain the entire collection of pages without doing any
  62. intermediary processing on each page, you can use the AllPages method:
  63. allPages, err := servers.List(client, nil).AllPages()
  64. allServers, err := servers.ExtractServers(allPages)
  65. This top-level package contains utility functions and data types that are used
  66. throughout the provider and service packages. Of particular note for end users
  67. are the AuthOptions and EndpointOpts structs.
  68. */
  69. package gophercloud