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