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

     1  package pivnet
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  )
     9  
    10  type UpgradePathSpecifiersService struct {
    11  	client Client
    12  }
    13  
    14  type UpgradePathSpecifiersResponse struct {
    15  	UpgradePathSpecifiers []UpgradePathSpecifier `json:"upgrade_path_specifiers,omitempty"`
    16  }
    17  
    18  type UpgradePathSpecifierResponse struct {
    19  	UpgradePathSpecifier UpgradePathSpecifier `json:"upgrade_path_specifier,omitempty"`
    20  }
    21  
    22  type UpgradePathSpecifier struct {
    23  	ID        int    `json:"id,omitempty" yaml:"id,omitempty"`
    24  	Specifier string `json:"specifier,omitempty" yaml:"specifier,omitempty"`
    25  }
    26  
    27  func (r UpgradePathSpecifiersService) List(productSlug string, releaseID int) ([]UpgradePathSpecifier, error) {
    28  	url := fmt.Sprintf(
    29  		"/products/%s/releases/%d/upgrade_path_specifiers",
    30  		productSlug,
    31  		releaseID,
    32  	)
    33  
    34  	var response UpgradePathSpecifiersResponse
    35  	resp, err := r.client.MakeRequest(
    36  		"GET",
    37  		url,
    38  		http.StatusOK,
    39  		nil,
    40  	)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	defer resp.Body.Close()
    45  
    46  	err = json.NewDecoder(resp.Body).Decode(&response)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	return response.UpgradePathSpecifiers, nil
    52  }
    53  
    54  func (r UpgradePathSpecifiersService) Get(productSlug string, releaseID int, upgradePathSpecifierID int) (UpgradePathSpecifier, error) {
    55  	url := fmt.Sprintf(
    56  		"/products/%s/releases/%d/upgrade_path_specifiers/%d",
    57  		productSlug,
    58  		releaseID,
    59  		upgradePathSpecifierID,
    60  	)
    61  
    62  	resp, err := r.client.MakeRequest(
    63  		"GET",
    64  		url,
    65  		http.StatusOK,
    66  		nil,
    67  	)
    68  	if err != nil {
    69  		return UpgradePathSpecifier{}, err
    70  	}
    71  	defer resp.Body.Close()
    72  
    73  	var response UpgradePathSpecifierResponse
    74  	err = json.NewDecoder(resp.Body).Decode(&response)
    75  	if err != nil {
    76  		return UpgradePathSpecifier{}, err
    77  	}
    78  
    79  	return response.UpgradePathSpecifier, nil
    80  }
    81  
    82  func (r UpgradePathSpecifiersService) Create(productSlug string, releaseID int, specifier string) (UpgradePathSpecifier, error) {
    83  	url := fmt.Sprintf(
    84  		"/products/%s/releases/%d/upgrade_path_specifiers",
    85  		productSlug,
    86  		releaseID,
    87  	)
    88  
    89  	body := createUpgradePathSpecifierBody{
    90  		createUpgradePathSpecifierBodyUpgradePathSpecifier{
    91  			Specifier: specifier,
    92  		},
    93  	}
    94  
    95  	b, err := json.Marshal(body)
    96  	if err != nil {
    97  		// Untested as we cannot force an error because we are marshalling
    98  		// a known-good body
    99  		return UpgradePathSpecifier{}, err
   100  	}
   101  
   102  	resp, err := r.client.MakeRequest(
   103  		"POST",
   104  		url,
   105  		http.StatusCreated,
   106  		bytes.NewReader(b),
   107  	)
   108  	if err != nil {
   109  		return UpgradePathSpecifier{}, err
   110  	}
   111  	defer resp.Body.Close()
   112  
   113  	var response UpgradePathSpecifierResponse
   114  	err = json.NewDecoder(resp.Body).Decode(&response)
   115  	if err != nil {
   116  		return UpgradePathSpecifier{}, err
   117  	}
   118  
   119  	return response.UpgradePathSpecifier, nil
   120  }
   121  
   122  func (r UpgradePathSpecifiersService) Delete(
   123  	productSlug string,
   124  	releaseID int,
   125  	upgradePathSpecifierID int,
   126  ) error {
   127  	url := fmt.Sprintf(
   128  		"/products/%s/releases/%d/upgrade_path_specifiers/%d",
   129  		productSlug,
   130  		releaseID,
   131  		upgradePathSpecifierID,
   132  	)
   133  
   134  	resp, err := r.client.MakeRequest(
   135  		"DELETE",
   136  		url,
   137  		http.StatusNoContent,
   138  		nil,
   139  	)
   140  	if err != nil {
   141  		return err
   142  	}
   143  	defer resp.Body.Close()
   144  
   145  	return nil
   146  }
   147  
   148  type createUpgradePathSpecifierBody struct {
   149  	UpgradePathSpecifier createUpgradePathSpecifierBodyUpgradePathSpecifier `json:"upgrade_path_specifier"`
   150  }
   151  
   152  type createUpgradePathSpecifierBodyUpgradePathSpecifier struct {
   153  	Specifier string `json:"specifier"`
   154  }