github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/README.md (about)

     1  # Gophercloud: an OpenStack SDK for Go
     2  [![Coverage Status](https://coveralls.io/repos/github/gophercloud/gophercloud/badge.svg?branch=master)](https://coveralls.io/github/gophercloud/gophercloud?branch=master)
     3  
     4  Gophercloud is a Go SDK for OpenStack.
     5  
     6  Join us on kubernetes slack, on [#gophercloud](https://kubernetes.slack.com/archives/C05G4NJ6P6X). Visit [slack.k8s.io](https://slack.k8s.io) for an invitation.
     7  
     8  > **Note**
     9  > This branch contains the current stable branch of Gophercloud: `v2`.
    10  > The legacy stable version can be found in the `v1` branch.
    11  
    12  ## How to install
    13  
    14  Reference a Gophercloud package in your code:
    15  
    16  ```go
    17  import "github.com/vnpaycloud-console/gophercloud/v2"
    18  ```
    19  
    20  Then update your `go.mod`:
    21  
    22  ```shell
    23  go mod tidy
    24  ```
    25  
    26  ## Getting started
    27  
    28  ### Credentials
    29  
    30  Because you'll be hitting an API, you will need to retrieve your OpenStack
    31  credentials and either store them in a `clouds.yaml` file, as environment
    32  variables, or in your local Go files. The first method is recommended because
    33  it decouples credential information from source code, allowing you to push the
    34  latter to your version control system without any security risk.
    35  
    36  You will need to retrieve the following:
    37  
    38  * A valid Keystone identity URL
    39  * Credentials. These can be a username/password combo, a set of Application
    40    Credentials, a pre-generated token, or any other supported authentication
    41    mechanism.
    42  
    43  For users who have the OpenStack dashboard installed, there's a shortcut. If
    44  you visit the `project/api_access` path in Horizon and click on the
    45  "Download OpenStack RC File" button at the top right hand corner, you can
    46  download either a `clouds.yaml` file or an `openrc` bash file that exports all
    47  of your access details to environment variables. To use the `clouds.yaml` file,
    48  place it at `~/.config/openstack/clouds.yaml`. To use the `openrc` file, run
    49  `source openrc` and you will be prompted for your password.
    50  
    51  ### Gophercloud authentication
    52  
    53  Gophercloud authentication is organized into two layered abstractions:
    54  * `ProviderClient` holds the authentication token and can be used to build a
    55    `ServiceClient`.
    56  * `ServiceClient` specializes against one specific OpenStack module and can
    57    directly be used to make API calls.
    58  
    59  A provider client is a top-level client that all of your OpenStack service
    60  clients derive from. The provider contains all of the authentication details
    61  that allow your Go code to access the API - such as the base URL and token ID.
    62  
    63  One single Provider client can be used to build as many Service clients as needed.
    64  
    65  **With `clouds.yaml`**
    66  
    67  ```go
    68  package main
    69  
    70  import (
    71  	"context"
    72  
    73  	"github.com/vnpaycloud-console/gophercloud/v2/openstack"
    74  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/config"
    75  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/config/clouds"
    76  )
    77  
    78  func main() {
    79  	ctx := context.Background()
    80  
    81  	// Fetch coordinates from a `cloud.yaml` in the current directory, or
    82  	// in the well-known config directories (different for each operating
    83  	// system).
    84  	authOptions, endpointOptions, tlsConfig, err := clouds.Parse()
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  
    89  	// Call Keystone to get an authentication token, and use it to
    90  	// construct a ProviderClient. All functions hitting the OpenStack API
    91  	// accept a `context.Context` to enable tracing and cancellation.
    92  	providerClient, err := config.NewProviderClient(ctx, authOptions, config.WithTLSConfig(tlsConfig))
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  
    97  	// Use the ProviderClient and the endpoint options fetched from
    98  	// `clouds.yaml` to build a service client: a compute client in this
    99  	// case. Note that the contructor does not accept a `context.Context`:
   100  	// no further call to the OpenStack API is needed at this stage.
   101  	computeClient, err := openstack.NewComputeV2(providerClient, endpointOptions)
   102  	if err != nil {
   103  		panic(err)
   104  	}
   105  
   106  	// use the computeClient
   107  }
   108  ```
   109  
   110  **With environment variables (`openrc`)**
   111  
   112  Gophercloud can parse the environment variables set by running `source openrc`:
   113  
   114  ```go
   115  package main
   116  
   117  import (
   118  	"context"
   119  	"os"
   120  
   121  	"github.com/vnpaycloud-console/gophercloud/v2"
   122  	"github.com/vnpaycloud-console/gophercloud/v2/openstack"
   123  )
   124  
   125  func main() {
   126  	ctx := context.Background()
   127  
   128  	opts, err := openstack.AuthOptionsFromEnv()
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	providerClient, err := openstack.AuthenticatedClient(ctx, opts)
   134  	if err != nil {
   135  		panic(err)
   136  	}
   137  
   138  	computeClient, err := openstack.NewComputeV2(providerClient, gophercloud.EndpointOpts{
   139  		Region: os.Getenv("OS_REGION_NAME"),
   140  	})
   141  	if err != nil {
   142  		panic(err)
   143  	}
   144  
   145  	// use the computeClient
   146  }
   147  ```
   148  
   149  **Manually**
   150  
   151  You can also generate a "Provider" by passing in your credentials
   152  explicitly:
   153  
   154  ```go
   155  package main
   156  
   157  import (
   158  	"context"
   159  
   160  	"github.com/vnpaycloud-console/gophercloud/v2"
   161  	"github.com/vnpaycloud-console/gophercloud/v2/openstack"
   162  )
   163  
   164  func main() {
   165  	ctx := context.Background()
   166  
   167  	providerClient, err := openstack.AuthenticatedClient(ctx, gophercloud.AuthOptions{
   168  		IdentityEndpoint: "https://openstack.example.com:5000/v2.0",
   169  		Username:         "username",
   170  		Password:         "password",
   171  	})
   172  	if err != nil {
   173  		panic(err)
   174  	}
   175  
   176  	computeClient, err := openstack.NewComputeV2(providerClient, gophercloud.EndpointOpts{
   177  		Region: "RegionName",
   178  	})
   179  	if err != nil {
   180  		panic(err)
   181  	}
   182  
   183  	// use the computeClient
   184  }
   185  ```
   186  
   187  ### Provision a server
   188  
   189  We can use the Compute service client generated above for any Compute API
   190  operation we want. In our case, we want to provision a new server. To do this,
   191  we invoke the `Create` method and pass in the flavor ID (hardware
   192  specification) and image ID (operating system) we're interested in:
   193  
   194  ```go
   195  import "github.com/vnpaycloud-console/gophercloud/v2/openstack/compute/v2/servers"
   196  
   197  func main() {
   198      // [...]
   199  
   200      server, err := servers.Create(context.TODO(), computeClient, servers.CreateOpts{
   201          Name:      "My new server!",
   202          FlavorRef: "flavor_id",
   203          ImageRef:  "image_id",
   204      }).Extract()
   205  
   206      // [...]
   207  ```
   208  
   209  The above code sample creates a new server with the parameters, and returns a
   210  [`servers.Server`](https://pkg.go.dev/github.com/vnpaycloud-console/gophercloud/v2/openstack/compute/v2/servers#Server).
   211  
   212  ## Supported Services
   213  
   214  |        **Service**       | **Name**         |             **Module**             | **1.x** | **2.x** |
   215  |:------------------------:|------------------|:----------------------------------:|:-------:|:-------:|
   216  |         Baremetal        |      Ironic      |        `openstack/baremetal`       |    ✔    |    ✔    |
   217  |  Baremetal Introspection | Ironic Inspector | `openstack/baremetalintrospection` |    ✔    |    ✔    |
   218  |       Block Storage      |      Cinder      |      `openstack/blockstorage`      |    ✔    |    ✔    |
   219  |        Clustering        |      Senlin      |       `openstack/clustering`       |    ✔    |    ✘    |
   220  |          Compute         |       Nova       |         `openstack/compute`        |    ✔    |    ✔    |
   221  |         Container        |        Zun       |        `openstack/container`       |    ✔    |    ✔    |
   222  | Container Infrastructure |      Magnum      |     `openstack/containerinfra`     |    ✔    |    ✔    |
   223  |         Database         |       Trove      |           `openstack/db`           |    ✔    |    ✔    |
   224  |            DNS           |     Designate    |           `openstack/dns`          |    ✔    |    ✔    |
   225  |         Identity         |     Keystone     |        `openstack/identity`        |    ✔    |    ✔    |
   226  |           Image          |      Glance      |          `openstack/image`         |    ✔    |    ✔    |
   227  |      Key Management      |     Barbican     |       `openstack/keymanager`       |    ✔    |    ✔    |
   228  |      Load Balancing      |      Octavia     |      `openstack/loadbalancer`      |    ✔    |    ✔    |
   229  |         Messaging        |       Zaqar      |        `openstack/messaging`       |    ✔    |    ✔    |
   230  |        Networking        |      Neutron     |       `openstack/networking`       |    ✔    |    ✔    |
   231  |      Object Storage      |       Swift      |      `openstack/objectstorage`     |    ✔    |    ✔    |
   232  
   233  ## Advanced Usage
   234  
   235  Have a look at the [FAQ](./docs/FAQ.md) for some tips on customizing the way Gophercloud works.
   236  
   237  ## Backwards-Compatibility Guarantees
   238  
   239  Gophercloud versioning follows [semver](https://semver.org/spec/v2.0.0.html).
   240  
   241  Before `v1.0.0`, there were no guarantees. Starting with v1, there will be no breaking changes within a major release.
   242  
   243  See the [Release instructions](./RELEASE.md).
   244  
   245  ## Contributing
   246  
   247  See the [contributing guide](./.github/CONTRIBUTING.md).
   248  
   249  ## Help and feedback
   250  
   251  If you're struggling with something or have spotted a potential bug, feel free
   252  to submit an issue to our [bug tracker](https://github.com/gophercloud/gophercloud/issues).