github.com/pivotal-cf/go-pivnet/v6@v6.0.2/federation_token.go (about)

     1  package pivnet
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  )
     9  
    10  type FederationTokenService struct {
    11  	client Client
    12  }
    13  
    14  type FederationToken struct {
    15  	AccessKeyID     string `json:"access_key_id,omitempty" yaml:"access_key_id,omitempty"`
    16  	SecretAccessKey string `json:"secret_access_key,omitempty" yaml:"secret_access_key,omitempty"`
    17  	SessionToken    string `json:"session_token,omitempty" yaml:"session_token,omitempty"`
    18  	Bucket          string `json:"bucket,omitempty" yaml:"bucket,omitempty"`
    19  	Region          string `json:"region,omitempty" yaml:"region,omitempty"`
    20  }
    21  
    22  type createFederationTokenBody struct {
    23  	ProductID string `json:"product_id"`
    24  }
    25  
    26  func (f FederationTokenService) GenerateFederationToken(productSlug string) (FederationToken, error) {
    27  	url := fmt.Sprintf("/federation_token")
    28  
    29  	body := createFederationTokenBody{
    30  		ProductID: productSlug,
    31  	}
    32  
    33  	b, err := json.Marshal(body)
    34  	if err != nil {
    35  		// Untested as we cannot force an error because we are marshalling
    36  		// a known-good body
    37  		return FederationToken{}, fmt.Errorf("could not marshall json: %v", err)
    38  	}
    39  
    40  	resp, err := f.client.MakeRequest(
    41  		"POST",
    42  		url,
    43  		http.StatusOK,
    44  		bytes.NewReader(b),
    45  	)
    46  
    47  	if err != nil {
    48  		return FederationToken{}, fmt.Errorf("error making federation token request: %v", err)
    49  	}
    50  	defer resp.Body.Close()
    51  
    52  	var token FederationToken
    53  	err = json.NewDecoder(resp.Body).Decode(&token)
    54  	if err != nil {
    55  		return FederationToken{}, fmt.Errorf("error decoding response body for federation token: %v", err)
    56  	}
    57  
    58  	return token, nil
    59  }