github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/build.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    10  )
    11  
    12  // Build represent the process of staging an application package.
    13  type Build struct {
    14  	// CreatedAt is the time with zone when the build was created.
    15  	CreatedAt string
    16  	// DropletGUID is the unique identifier for the resulting droplet from the
    17  	// staging process.
    18  	DropletGUID string
    19  	// Error describes errors during the build process.
    20  	Error string
    21  	// GUID is the unique build identifier.
    22  	GUID string
    23  	// PackageGUID is the unique identifier for package that is the input to the
    24  	// staging process.
    25  	PackageGUID string
    26  	// State is the state of the build.
    27  	State constant.BuildState
    28  }
    29  
    30  // MarshalJSON converts a Build into a Cloud Controller Application.
    31  func (b Build) MarshalJSON() ([]byte, error) {
    32  	var ccBuild struct {
    33  		Package struct {
    34  			GUID string `json:"guid"`
    35  		} `json:"package"`
    36  	}
    37  
    38  	ccBuild.Package.GUID = b.PackageGUID
    39  
    40  	return json.Marshal(ccBuild)
    41  }
    42  
    43  // UnmarshalJSON helps unmarshal a Cloud Controller Build response.
    44  func (b *Build) UnmarshalJSON(data []byte) error {
    45  	var ccBuild struct {
    46  		CreatedAt string `json:"created_at,omitempty"`
    47  		GUID      string `json:"guid,omitempty"`
    48  		Error     string `json:"error"`
    49  		Package   struct {
    50  			GUID string `json:"guid"`
    51  		} `json:"package"`
    52  		State   constant.BuildState `json:"state,omitempty"`
    53  		Droplet struct {
    54  			GUID string `json:"guid"`
    55  		} `json:"droplet"`
    56  	}
    57  
    58  	err := cloudcontroller.DecodeJSON(data, &ccBuild)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	b.GUID = ccBuild.GUID
    64  	b.CreatedAt = ccBuild.CreatedAt
    65  	b.Error = ccBuild.Error
    66  	b.PackageGUID = ccBuild.Package.GUID
    67  	b.State = ccBuild.State
    68  	b.DropletGUID = ccBuild.Droplet.GUID
    69  
    70  	return nil
    71  }
    72  
    73  // CreateBuild creates the given build, requires Package GUID to be set on the
    74  // build.
    75  func (client *Client) CreateBuild(build Build) (Build, Warnings, error) {
    76  	bodyBytes, err := json.Marshal(build)
    77  	if err != nil {
    78  		return Build{}, nil, err
    79  	}
    80  
    81  	request, err := client.newHTTPRequest(requestOptions{
    82  		RequestName: internal.PostBuildRequest,
    83  		Body:        bytes.NewReader(bodyBytes),
    84  	})
    85  	if err != nil {
    86  		return Build{}, nil, err
    87  	}
    88  
    89  	var responseBuild Build
    90  	response := cloudcontroller.Response{
    91  		DecodeJSONResponseInto: &responseBuild,
    92  	}
    93  	err = client.connection.Make(request, &response)
    94  
    95  	return responseBuild, response.Warnings, err
    96  }
    97  
    98  // GetBuild gets the build with the given GUID.
    99  func (client *Client) GetBuild(guid string) (Build, Warnings, error) {
   100  	request, err := client.newHTTPRequest(requestOptions{
   101  		RequestName: internal.GetBuildRequest,
   102  		URIParams:   internal.Params{"build_guid": guid},
   103  	})
   104  	if err != nil {
   105  		return Build{}, nil, err
   106  	}
   107  
   108  	var responseBuild Build
   109  	response := cloudcontroller.Response{
   110  		DecodeJSONResponseInto: &responseBuild,
   111  	}
   112  	err = client.connection.Make(request, &response)
   113  
   114  	return responseBuild, response.Warnings, err
   115  }