github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/resources/build_resource.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     8  )
     9  
    10  // Build represent the process of staging an application package.
    11  type Build struct {
    12  	// CreatedAt is the time with zone when the build was created.
    13  	CreatedAt string
    14  	// DropletGUID is the unique identifier for the resulting droplet from the
    15  	// staging process.
    16  	DropletGUID string
    17  	// Error describes errors during the build process.
    18  	Error string
    19  	// GUID is the unique build identifier.
    20  	GUID string
    21  	// PackageGUID is the unique identifier for package that is the input to the
    22  	// staging process.
    23  	PackageGUID string
    24  	// State is the state of the build.
    25  	State constant.BuildState
    26  }
    27  
    28  // MarshalJSON converts a Build into a Cloud Controller Application.
    29  func (b Build) MarshalJSON() ([]byte, error) {
    30  	var ccBuild struct {
    31  		Package struct {
    32  			GUID string `json:"guid"`
    33  		} `json:"package"`
    34  	}
    35  
    36  	ccBuild.Package.GUID = b.PackageGUID
    37  
    38  	return json.Marshal(ccBuild)
    39  }
    40  
    41  // UnmarshalJSON helps unmarshal a Cloud Controller Build response.
    42  func (b *Build) UnmarshalJSON(data []byte) error {
    43  	var ccBuild struct {
    44  		CreatedAt string `json:"created_at,omitempty"`
    45  		GUID      string `json:"guid,omitempty"`
    46  		Error     string `json:"error"`
    47  		Package   struct {
    48  			GUID string `json:"guid"`
    49  		} `json:"package"`
    50  		State   constant.BuildState `json:"state,omitempty"`
    51  		Droplet struct {
    52  			GUID string `json:"guid"`
    53  		} `json:"droplet"`
    54  	}
    55  
    56  	err := cloudcontroller.DecodeJSON(data, &ccBuild)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	b.GUID = ccBuild.GUID
    62  	b.CreatedAt = ccBuild.CreatedAt
    63  	b.Error = ccBuild.Error
    64  	b.PackageGUID = ccBuild.Package.GUID
    65  	b.State = ccBuild.State
    66  	b.DropletGUID = ccBuild.Droplet.GUID
    67  
    68  	return nil
    69  }