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