github.com/fastly/go-fastly@v1.18.0/README.md (about)

     1  Go Fastly
     2  =========
     3  [![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs]
     4  
     5  [godocs]: https://pkg.go.dev/github.com/fastly/go-fastly/fastly?tab=doc
     6  
     7  Go Fastly is a Golang API client for interacting with most facets of the
     8  [Fastly API](https://docs.fastly.com/api).
     9  
    10  Installation
    11  ------------
    12  This is a client library, so there is nothing to install. But, it uses Go modules,
    13  so you must be running Go 1.11 or higher.
    14  
    15  Usage
    16  -----
    17  Fetch the library:
    18  
    19  ```
    20  $ go get github.com/fastly/go-fastly/fastly
    21  ```
    22  
    23  Import the library into your tool:
    24  
    25  ```go
    26  import "github.com/fastly/go-fastly/fastly"
    27  ```
    28  
    29  Examples
    30  --------
    31  Fastly's API is designed to work in the following manner:
    32  
    33  1. Create (or clone) a new configuration version for the service
    34  2. Make any changes to the version
    35  3. Validate the version
    36  4. Activate the version
    37  
    38  This flow using the Golang client looks like this:
    39  
    40  ```go
    41  // Create a client object. The client has no state, so it can be persisted
    42  // and re-used. It is also safe to use concurrently due to its lack of state.
    43  // There is also a DefaultClient() method that reads an environment variable.
    44  // Please see the documentation for more information and details.
    45  client, err := fastly.NewClient("YOUR_FASTLY_API_KEY")
    46  if err != nil {
    47    log.Fatal(err)
    48  }
    49  
    50  // You can find the service ID in the Fastly web console.
    51  var serviceID = "SERVICE_ID"
    52  
    53  // Get the latest active version
    54  latest, err := client.LatestVersion(&fastly.LatestVersionInput{
    55    Service: serviceID,
    56  })
    57  if err != nil {
    58    log.Fatal(err)
    59  }
    60  
    61  // Clone the latest version so we can make changes without affecting the
    62  // active configuration.
    63  version, err := client.CloneVersion(&fastly.CloneVersionInput{
    64    Service: serviceID,
    65    Version: latest.Number,
    66  })
    67  if err != nil {
    68    log.Fatal(err)
    69  }
    70  
    71  // Now you can make any changes to the new version. In this example, we will add
    72  // a new domain.
    73  domain, err := client.CreateDomain(&fastly.CreateDomainInput{
    74    Service: serviceID,
    75    Version: version.Number,
    76    Name: "example.com",
    77  })
    78  if err != nil {
    79    log.Fatal(err)
    80  }
    81  
    82  // Output: "example.com"
    83  fmt.Println(domain.Name)
    84  
    85  // And we will also add a new backend.
    86  backend, err := client.CreateBackend(&fastly.CreateBackendInput{
    87    Service: serviceID,
    88    Version: version.Number,
    89    Name:    "example-backend",
    90    Address: "127.0.0.1",
    91    Port:    80,
    92  })
    93  if err != nil {
    94    log.Fatal(err)
    95  }
    96  
    97  // Output: "example-backend"
    98  fmt.Println(backend.Name)
    99  
   100  // Now we can validate that our version is valid.
   101  valid, _, err := client.ValidateVersion(&fastly.ValidateVersionInput{
   102    Service: serviceID,
   103    Version: version.Number,
   104  })
   105  if err != nil {
   106    log.Fatal(err)
   107  }
   108  if !valid {
   109    log.Fatal("not valid version")
   110  }
   111  
   112  // Finally, activate this new version.
   113  activeVersion, err := client.ActivateVersion(&fastly.ActivateVersionInput{
   114    Service: serviceID,
   115    Version: version.Number,
   116  })
   117  if err != nil {
   118    log.Fatal(err)
   119  }
   120  
   121  // Output: true
   122  fmt.Printf("%t\n", activeVersion.Locked)
   123  ```
   124  
   125  More information can be found in the
   126  [Fastly Godoc](https://godoc.org/github.com/fastly/go-fastly).
   127  
   128  Developing
   129  -------
   130  
   131  1. Clone the project to your preferred directory, using your preferred method.
   132  2. Download the module and accompanying developer tooling.
   133  
   134    ```bash
   135    $ go mod download
   136    ```
   137  
   138  3. Make changes.
   139  4. Verify those changes.
   140  
   141    ```bash
   142    $ make all
   143    ```
   144  
   145  Testing
   146  -------
   147  Go Fastly uses [go-vcr](https://github.com/dnaeon/go-vcr) to "record" and
   148  "replay" API request fixtures to improve the speed and portability of
   149  integration tests. The test suite uses a single test service ID for all test
   150  fixtures.
   151  
   152  Contributors _without_ access to the test service can disable go-vcr, set a
   153  different test service ID, and use their own API credentials by setting the
   154  following environment variables:
   155  * `VCR_DISABLE`: disables go-vcr fixture recording and replay
   156    * The `test-full` make target runs the tests with go-vcr disabled.
   157  * `FASTLY_TEST_SERVICE_ID`: set default service ID used by the test suite
   158    * **NOTE: A quick way to create a resource for testing is to use the following [Fastly CLI](https://github.com/fastly/cli) command:**
   159    ```
   160    $ fastly service create --type=vcl --name=foo
   161    ```
   162  * `FASTLY_API_KEY`: set a Fastly API key to be used by the test suite
   163  
   164  ```sh
   165  make test-full \
   166    FASTLY_TEST_SERVICE_ID="SERVICEID" \
   167    FASTLY_API_KEY="TESTAPIKEY" \
   168    TESTARGS="-run=Logentries"
   169  ```
   170  
   171  When adding or updating client code and integration tests, contributors may
   172  record a new set of fixtures by running the tests without `VCR_DISABLE`. Before
   173  submitting a pull request with new or updated fixtures, we ask that contributors
   174  update them to use the default service ID by running `make fix-fixtures` with
   175  `FASTLY_TEST_SERVICE_ID` set to the same value used to run your tests.
   176  
   177  If you are **updating** an existing resource, in order to force go-vcr to re-record,
   178  rather than use the existing recorded values, you will have to removed the recorded
   179  values for that resource via `rm -r fastly/fixtures/<resource_name>`. **If you don't
   180  remove the existing recorded values, you will see the following error**:
   181  
   182  ```
   183  Post "https://api.fastly.com/service/SERVICEID/version": Requested interaction not found
   184  ```
   185  
   186  Example (`make test`, `make fix-fixtures`):
   187  ```sh
   188  make test \
   189    FASTLY_TEST_SERVICE_ID="SERVICEID" \
   190    FASTLY_API_KEY="TESTAPIKEY" \
   191    TESTARGS="-run=Logentries"
   192  make fix-fixtures
   193  
   194  # Re-run test suite with newly recorded fixtures
   195  unset FASTLY_TEST_SERVICE_ID FASTLY_API_KEY
   196  make test
   197  ```
   198  
   199  Contributing
   200  --------------------------
   201  
   202  Refer to [CONTRIBUTING.md](./CONTRIBUTING.md)
   203  
   204  License
   205  -------
   206  ```
   207  Copyright 2015 Seth Vargo
   208  
   209  Licensed under the Apache License, Version 2.0 (the "License");
   210  you may not use this file except in compliance with the License.
   211  You may obtain a copy of the License at
   212  
   213      http://www.apache.org/licenses/LICENSE-2.0
   214  
   215  Unless required by applicable law or agreed to in writing, software
   216  distributed under the License is distributed on an "AS IS" BASIS,
   217  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   218  See the License for the specific language governing permissions and
   219  limitations under the License.
   220  ```