github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/build.go (about)

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