github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/resources/buildpack_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/types"
     8  )
     9  
    10  // Buildpack represents a Cloud Controller V3 buildpack.
    11  type Buildpack struct {
    12  	// Enabled is true when the buildpack can be used for staging.
    13  	Enabled types.NullBool
    14  	// Filename is the uploaded filename of the buildpack.
    15  	Filename string
    16  	// GUID is the unique identifier for the buildpack.
    17  	GUID string
    18  	// Locked is true when the buildpack cannot be updated.
    19  	Locked types.NullBool
    20  	// Name is the name of the buildpack. To be used by app buildpack field.
    21  	// (only alphanumeric characters)
    22  	Name string
    23  	// Position is the order in which the buildpacks are checked during buildpack
    24  	// auto-detection.
    25  	Position types.NullInt
    26  	// Stack is the name of the stack that the buildpack will use.
    27  	Stack string
    28  	// State is the current state of the buildpack.
    29  	State string
    30  	// Links are links to related resources.
    31  	Links APILinks
    32  	// Metadata is used for custom tagging of API resources
    33  	Metadata *Metadata
    34  }
    35  
    36  // MarshalJSON converts a Package into a Cloud Controller Package.
    37  func (buildpack Buildpack) MarshalJSON() ([]byte, error) {
    38  	ccBuildpack := struct {
    39  		Name     string    `json:"name,omitempty"`
    40  		Stack    string    `json:"stack,omitempty"`
    41  		Position *int      `json:"position,omitempty"`
    42  		Enabled  *bool     `json:"enabled,omitempty"`
    43  		Locked   *bool     `json:"locked,omitempty"`
    44  		Metadata *Metadata `json:"metadata,omitempty"`
    45  	}{
    46  		Name:  buildpack.Name,
    47  		Stack: buildpack.Stack,
    48  	}
    49  
    50  	if buildpack.Position.IsSet {
    51  		ccBuildpack.Position = &buildpack.Position.Value
    52  	}
    53  	if buildpack.Enabled.IsSet {
    54  		ccBuildpack.Enabled = &buildpack.Enabled.Value
    55  	}
    56  	if buildpack.Locked.IsSet {
    57  		ccBuildpack.Locked = &buildpack.Locked.Value
    58  	}
    59  
    60  	return json.Marshal(ccBuildpack)
    61  }
    62  
    63  func (buildpack *Buildpack) UnmarshalJSON(data []byte) error {
    64  	var ccBuildpack struct {
    65  		GUID     string         `json:"guid,omitempty"`
    66  		Links    APILinks       `json:"links,omitempty"`
    67  		Name     string         `json:"name,omitempty"`
    68  		Filename string         `json:"filename,omitempty"`
    69  		Stack    string         `json:"stack,omitempty"`
    70  		State    string         `json:"state,omitempty"`
    71  		Enabled  types.NullBool `json:"enabled"`
    72  		Locked   types.NullBool `json:"locked"`
    73  		Position types.NullInt  `json:"position"`
    74  		Metadata *Metadata      `json:"metadata"`
    75  	}
    76  
    77  	err := cloudcontroller.DecodeJSON(data, &ccBuildpack)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	buildpack.Enabled = ccBuildpack.Enabled
    83  	buildpack.Filename = ccBuildpack.Filename
    84  	buildpack.GUID = ccBuildpack.GUID
    85  	buildpack.Locked = ccBuildpack.Locked
    86  	buildpack.Name = ccBuildpack.Name
    87  	buildpack.Position = ccBuildpack.Position
    88  	buildpack.Stack = ccBuildpack.Stack
    89  	buildpack.State = ccBuildpack.State
    90  	buildpack.Links = ccBuildpack.Links
    91  	buildpack.Metadata = ccBuildpack.Metadata
    92  
    93  	return nil
    94  }