github.com/fastly/go-fastly/v5@v5.3.0/README.md (about)

     1  # Go Fastly
     2  
     3  [![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][latest]
     4  
     5  [latest]: https://pkg.go.dev/github.com/fastly/go-fastly/v5/fastly
     6  [v5]: https://pkg.go.dev/github.com/fastly/go-fastly/v5/fastly
     7  [v4]: https://pkg.go.dev/github.com/fastly/go-fastly/v4/fastly
     8  [v3]: https://pkg.go.dev/github.com/fastly/go-fastly/v3/fastly
     9  [v2]: https://pkg.go.dev/github.com/fastly/go-fastly/v2/fastly
    10  [v1]: https://pkg.go.dev/github.com/fastly/go-fastly
    11  
    12  Go Fastly is a Golang API client for interacting with most facets of the
    13  [Fastly API](https://docs.fastly.com/api).
    14  
    15  ## Installation
    16  
    17  This is a client library, so there is nothing to install. But, it uses Go modules,
    18  so you must be running Go 1.11 or higher.
    19  
    20  ## Usage
    21  
    22  ```go
    23  import "github.com/fastly/go-fastly/v5/fastly"
    24  ```
    25  
    26  ## Migrating from v1 to v2
    27  
    28  The move from major version [1][v1] to [2][v2] has resulted in a couple of fundamental changes to the library:
    29  
    30  - Consistent field name format for IDs and Versions (e.g. `DictionaryID`, `PoolID`, `ServiceID`, `ServiceVersion` etc).
    31  - Input struct fields (for write/update operations) that are optional (i.e. `omitempty`) and use basic types, are now defined as pointers.
    32  
    33  The move to more consistent field names in some cases will have resulted in the corresponding sentinel error name to be updated also. For example, `ServiceID` has resulted in a change from `ErrMissingService` to `ErrMissingServiceID`.
    34  
    35  The change in type for [basic types](https://tour.golang.org/basics/11) that are optional on input structs related to write/update operations is designed to avoid unexpected behaviours when dealing with their zero value (see [this reference](https://willnorris.com/2014/05/go-rest-apis-and-pointers/) for more details). As part of this change we now provide [helper functions](./fastly/basictypes_helper.go) to assist with generating the new pointer types required.
    36  
    37  > Note: some read/list operations require fields to be provided but if omitted a zero value will be used when marshaling the data structure into JSON. This too can cause confusion, which is why some input structs define their mandatory fields as pointers (to ensure that the backend can distinguish between a zero value and an omitted field).
    38  
    39  ## Migrating from v2 to v3
    40  
    41  There were a few breaking changes introduced in [`v3.0.0`][v3]:
    42  
    43  1. A new `FieldError` abstraction for validating API struct fields.
    44  2. Changing some mandatory fields to Optional (and vice-versa) to better support more _practical_ API usage.
    45  3. Avoid generic ID field when more explicit naming would be clearer.
    46  
    47  ## Examples
    48  
    49  Fastly's API is designed to work in the following manner:
    50  
    51  1. Create (or clone) a new configuration version for the service
    52  2. Make any changes to the version
    53  3. Validate the version
    54  4. Activate the version
    55  
    56  This flow using the Golang client looks like this:
    57  
    58  ```go
    59  package main
    60  
    61  import (
    62  	"fmt"
    63  	"log"
    64   	"os"
    65  	"github.com/fastly/go-fastly/v5/fastly"
    66  )
    67  
    68  func main() {
    69  	// Create a client object. The client has no state, so it can be persisted
    70  	// and re-used. It is also safe to use concurrently due to its lack of state.
    71  	// There is also a DefaultClient() method that reads an environment variable.
    72  	// Please see the documentation for more information and details.
    73  	client, err := fastly.NewClient(os.Getenv("FASTLY_API_KEY"))
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  
    78  	// You can find the service ID in the Fastly web console.
    79  	var serviceID = "SERVICE_ID"
    80  
    81  	// We'll get the latest 'active' version by inspecting the service metadata and
    82  	// then finding which available version is the 'active' version.
    83  	service, err := client.GetService(&fastly.GetServiceInput{
    84  		ID: serviceID,
    85  	})
    86  	if err != nil {
    87  		log.Fatal(err)
    88  	}
    89  
    90  	// Let's acquire a service version to clone from. We'll start by searching for
    91  	// the latest 'active' version available, and if there are no active versions,
    92  	// then we'll clone from whatever is the latest version.
    93  	latest := service.Versions[len(service.Versions)-1]
    94  	for _, version := range service.Versions {
    95  		if version.Active {
    96  			latest = version
    97  			break
    98  		}
    99  	}
   100  
   101  	// Clone the latest version so we can make changes without affecting the
   102  	// active configuration.
   103  	version, err := client.CloneVersion(&fastly.CloneVersionInput{
   104  		ServiceID:      serviceID,
   105  		ServiceVersion: latest.Number,
   106  	})
   107  	if err != nil {
   108  		log.Fatal(err)
   109  	}
   110  
   111  	// Now you can make any changes to the new version. In this example, we will add
   112  	// a new domain.
   113  	domain, err := client.CreateDomain(&fastly.CreateDomainInput{
   114  		ServiceID:      serviceID,
   115  		ServiceVersion: version.Number,
   116  		Name:           "example.com",
   117  	})
   118  	if err != nil {
   119  		log.Fatal(err)
   120  	}
   121  
   122  	// Output: "example.com"
   123  	fmt.Println("domain.Name:", domain.Name)
   124  
   125  	// And we will also add a new backend.
   126  	backend, err := client.CreateBackend(&fastly.CreateBackendInput{
   127  		ServiceID:      serviceID,
   128  		ServiceVersion: version.Number,
   129  		Name:           "example-backend",
   130  		Address:        "127.0.0.1",
   131  		Port:           80,
   132  	})
   133  	if err != nil {
   134  		log.Fatal(err)
   135  	}
   136  
   137  	// Output: "example-backend"
   138  	fmt.Println("backend.Name:", backend.Name)
   139  
   140  	// Now we can validate that our version is valid.
   141  	valid, _, err := client.ValidateVersion(&fastly.ValidateVersionInput{
   142  		ServiceID:      serviceID,
   143  		ServiceVersion: version.Number,
   144  	})
   145  	if err != nil {
   146  		log.Fatal(err)
   147  	}
   148  	if !valid {
   149  		log.Fatal("not valid version")
   150  	}
   151  
   152  	// Finally, activate this new version.
   153  	activeVersion, err := client.ActivateVersion(&fastly.ActivateVersionInput{
   154  		ServiceID:      serviceID,
   155  		ServiceVersion: version.Number,
   156  	})
   157  	if err != nil {
   158  		log.Fatal(err)
   159  	}
   160  
   161  	// Output: true
   162  	fmt.Println("activeVersion.Locked:", activeVersion.Locked)
   163  }
   164  ```
   165  
   166  More information can be found in the
   167  [Fastly Godoc][latest].
   168  
   169  ## Developing
   170  
   171  1. Clone the project to your preferred directory, using your preferred method.
   172  2. Download the module and accompanying developer tooling.
   173  
   174    ```bash
   175    $ go mod download
   176    ```
   177  
   178  3. Make changes.
   179  4. Verify those changes.
   180  
   181    ```bash
   182    $ make all
   183    ```
   184  
   185  ## Testing
   186  
   187  Go Fastly uses [go-vcr](https://github.com/dnaeon/go-vcr) to "record" and "replay" API request fixtures to improve the speed and portability of integration tests. The test suite uses a single test service ID for all test fixtures.
   188  
   189  Contributors without access to the test service can still update the fixtures but with some additional steps required. Below is an example workflow for updating a set of fixture files (where `...` should be replaced with an appropriate value):
   190  
   191  ```sh
   192  # Remove all yaml fixture files from the specified directory.
   193  #
   194  rm -r fastly/fixtures/.../*
   195  
   196  # Run a subsection of the tests.
   197  # This will cause the deleted fixtures to be recreated.
   198  # 
   199  # FASTLY_TEST_SERVICE_ID: should correspond to a real service you control.
   200  # FASTLY_API_KEY: should be a real token associated with the Service you control.
   201  # TESTARGS: allows you to use the -run flag of the 'go test' command.
   202  # 
   203  make test FASTLY_TEST_SERVICE_ID="..." FASTLY_API_KEY="..." TESTARGS="-run=..."
   204  ```
   205  
   206  > **NOTE**: to run the tests with go-vcr disabled, set `VCR_DISABLE=1` (`make test-full` does this).
   207  
   208  When adding or updating client code and integration tests, contributors should record a new set of fixtures. Before submitting a pull request with new or updated fixtures, we ask that contributors update them to use the default service ID by running `make fix-fixtures` with `FASTLY_TEST_SERVICE_ID` set to the same value used to run your tests.
   209  
   210  ```sh
   211  make fix-fixtures FASTLY_TEST_SERVICE_ID="..."
   212  ```
   213  
   214  ### Important Test Tips!
   215  
   216  There are two important things external contributors need to do when running the tests:
   217  
   218  1. Use a 'temporary' token for running the tests (only if regenerating the token fixtures).
   219  2. Redact sensitive information in your fixtures.
   220  
   221  You only need to use a temporary token when regenerating the 'token' fixtures. This is because there is a test to validate the _revoking_ of a token using the [`/tokens/self`](https://developer.fastly.com/reference/api/auth/#revoke-token-current) API endpoint, for which running this test (if there are no existing fixtures) will cause the token you provided at your command-line shell to be revoked/expired. So please don't use a token that's also used by a real/running application! Otherwise you'll discover those application may stop working as you've inadvertently caused your token to be revoked.
   222  
   223  In general, any time you regenerate fixtures you should be sure to redact any sensitive information served back from the API, but specifically there is a test which _creates_ tokens that needs special attention: when regenerating the token fixtures this will require you to enter your actual account credentials (i.e. username/password) into the `token_test.go` file. You'll want to ensure that once the fixtures are create that you redact those values from both the generated fixture as well as the go test file itself. For example...
   224  
   225  ```go
   226  input := &CreateTokenInput{
   227  	Name:     "my-test-token",
   228  	Scope:    "global",
   229  	Username: "XXXXXXXXXXXXXXXXXXXXXX",
   230  	Password: "XXXXXXXXXXXXXXXXXXXXXX",
   231  }
   232  ```
   233  
   234  ## Contributing
   235  
   236  Refer to [CONTRIBUTING.md](./CONTRIBUTING.md)
   237  
   238  ## License
   239  
   240  ```
   241  Copyright 2015 Seth Vargo
   242  
   243  Licensed under the Apache License, Version 2.0 (the "License");
   244  you may not use this file except in compliance with the License.
   245  You may obtain a copy of the License at
   246  
   247      http://www.apache.org/licenses/LICENSE-2.0
   248  
   249  Unless required by applicable law or agreed to in writing, software
   250  distributed under the License is distributed on an "AS IS" BASIS,
   251  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   252  See the License for the specific language governing permissions and
   253  limitations under the License.
   254  ```