github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/api/cloudcontroller/ccv3/deployment.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  	"code.cloudfoundry.org/cli/resources"
    10  )
    11  
    12  type Deployment struct {
    13  	GUID          string
    14  	State         constant.DeploymentState
    15  	StatusValue   constant.DeploymentStatusValue
    16  	StatusReason  constant.DeploymentStatusReason
    17  	RevisionGUID  string
    18  	DropletGUID   string
    19  	CreatedAt     string
    20  	UpdatedAt     string
    21  	Relationships resources.Relationships
    22  	NewProcesses  []Process
    23  }
    24  
    25  // MarshalJSON converts a Deployment into a Cloud Controller Deployment.
    26  func (d Deployment) MarshalJSON() ([]byte, error) {
    27  	type Revision struct {
    28  		GUID string `json:"guid,omitempty"`
    29  	}
    30  	type Droplet struct {
    31  		GUID string `json:"guid,omitempty"`
    32  	}
    33  
    34  	var ccDeployment struct {
    35  		Droplet       *Droplet                `json:"droplet,omitempty"`
    36  		Revision      *Revision               `json:"revision,omitempty"`
    37  		Relationships resources.Relationships `json:"relationships,omitempty"`
    38  	}
    39  
    40  	if d.DropletGUID != "" {
    41  		ccDeployment.Droplet = &Droplet{d.DropletGUID}
    42  	}
    43  
    44  	if d.RevisionGUID != "" {
    45  		ccDeployment.Revision = &Revision{d.RevisionGUID}
    46  	}
    47  
    48  	ccDeployment.Relationships = d.Relationships
    49  
    50  	return json.Marshal(ccDeployment)
    51  }
    52  
    53  // UnmarshalJSON helps unmarshal a Cloud Controller Deployment response.
    54  func (d *Deployment) UnmarshalJSON(data []byte) error {
    55  	var ccDeployment struct {
    56  		GUID          string                   `json:"guid,omitempty"`
    57  		CreatedAt     string                   `json:"created_at,omitempty"`
    58  		Relationships resources.Relationships  `json:"relationships,omitempty"`
    59  		State         constant.DeploymentState `json:"state,omitempty"`
    60  		Status        struct {
    61  			Value  constant.DeploymentStatusValue  `json:"value"`
    62  			Reason constant.DeploymentStatusReason `json:"reason"`
    63  		} `json:"status"`
    64  		Droplet      resources.Droplet `json:"droplet,omitempty"`
    65  		NewProcesses []Process         `json:"new_processes,omitempty"`
    66  	}
    67  	err := cloudcontroller.DecodeJSON(data, &ccDeployment)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	d.GUID = ccDeployment.GUID
    73  	d.CreatedAt = ccDeployment.CreatedAt
    74  	d.Relationships = ccDeployment.Relationships
    75  	d.State = ccDeployment.State
    76  	d.StatusValue = ccDeployment.Status.Value
    77  	d.StatusReason = ccDeployment.Status.Reason
    78  	d.DropletGUID = ccDeployment.Droplet.GUID
    79  	d.NewProcesses = ccDeployment.NewProcesses
    80  
    81  	return nil
    82  }
    83  
    84  func (client *Client) CancelDeployment(deploymentGUID string) (Warnings, error) {
    85  	_, warnings, err := client.MakeRequest(RequestParams{
    86  		RequestName: internal.PostApplicationDeploymentActionCancelRequest,
    87  		URIParams:   internal.Params{"deployment_guid": deploymentGUID},
    88  	})
    89  
    90  	return warnings, err
    91  }
    92  
    93  func (client *Client) CreateApplicationDeployment(appGUID string, dropletGUID string) (string, Warnings, error) {
    94  	dep := Deployment{
    95  		DropletGUID:   dropletGUID,
    96  		Relationships: resources.Relationships{constant.RelationshipTypeApplication: resources.Relationship{GUID: appGUID}},
    97  	}
    98  
    99  	var responseBody Deployment
   100  
   101  	_, warnings, err := client.MakeRequest(RequestParams{
   102  		RequestName:  internal.PostApplicationDeploymentRequest,
   103  		RequestBody:  dep,
   104  		ResponseBody: &responseBody,
   105  	})
   106  
   107  	return responseBody.GUID, warnings, err
   108  }
   109  
   110  func (client *Client) CreateApplicationDeploymentByRevision(appGUID string, revisionGUID string) (string, Warnings, error) {
   111  	dep := Deployment{
   112  		RevisionGUID:  revisionGUID,
   113  		Relationships: resources.Relationships{constant.RelationshipTypeApplication: resources.Relationship{GUID: appGUID}},
   114  	}
   115  
   116  	var responseBody Deployment
   117  
   118  	_, warnings, err := client.MakeRequest(RequestParams{
   119  		RequestName:  internal.PostApplicationDeploymentRequest,
   120  		RequestBody:  dep,
   121  		ResponseBody: &responseBody,
   122  	})
   123  
   124  	return responseBody.GUID, warnings, err
   125  }
   126  
   127  func (client *Client) GetDeployment(deploymentGUID string) (Deployment, Warnings, error) {
   128  	var responseBody Deployment
   129  
   130  	_, warnings, err := client.MakeRequest(RequestParams{
   131  		RequestName:  internal.GetDeploymentRequest,
   132  		URIParams:    internal.Params{"deployment_guid": deploymentGUID},
   133  		ResponseBody: &responseBody,
   134  	})
   135  
   136  	return responseBody, warnings, err
   137  }
   138  
   139  func (client *Client) GetDeployments(query ...Query) ([]Deployment, Warnings, error) {
   140  	var resources []Deployment
   141  
   142  	_, warnings, err := client.MakeListRequest(RequestParams{
   143  		RequestName:  internal.GetDeploymentsRequest,
   144  		Query:        query,
   145  		ResponseBody: Deployment{},
   146  		AppendToList: func(item interface{}) error {
   147  			resources = append(resources, item.(Deployment))
   148  			return nil
   149  		},
   150  	})
   151  
   152  	return resources, warnings, err
   153  }