Skip to content

Commit

Permalink
Merge pull request #234 from nutanix/task-completion-project
Browse files Browse the repository at this point in the history
Task completion project
  • Loading branch information
marinsalinas authored Jan 22, 2021
2 parents 8406709 + a48168f commit 927d91f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
9 changes: 5 additions & 4 deletions client/v3/v3_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Service interface {
ListProject(getEntitiesRequest *DSMetadata) (*ProjectListResponse, error)
ListAllProject() (*ProjectListResponse, error)
UpdateProject(uuid string, body *Project) (*Project, error)
DeleteProject(uuid string) error
DeleteProject(uuid string) (*DeleteResponse, error)
CreateAccessControlPolicy(request *AccessControlPolicy) (*AccessControlPolicy, error)
GetAccessControlPolicy(accessControlPolicyUUID string) (*AccessControlPolicy, error)
ListAccessControlPolicy(getEntitiesRequest *DSMetadata) (*AccessControlPolicyListResponse, error)
Expand Down Expand Up @@ -1308,17 +1308,18 @@ func (op Operations) UpdateProject(uuid string, body *Project) (*Project, error)
* @param uuid The uuid of the entity.
* @return void
*/
func (op Operations) DeleteProject(uuid string) error {
func (op Operations) DeleteProject(uuid string) (*DeleteResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/projects/%s", uuid)

req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)
deleteResponse := new(DeleteResponse)
if err != nil {
return err
return nil, err
}

return op.client.Do(ctx, req, nil)
return deleteResponse, op.client.Do(ctx, req, deleteResponse)
}

/*CreateAccessControlPolicy creates a access policy
Expand Down
19 changes: 18 additions & 1 deletion client/v3/v3_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3306,6 +3306,23 @@ func TestOperations_DeleteProject(t *testing.T) {

mux.HandleFunc("/api/nutanix/v3/projects/cfde831a-4e87-4a75-960f-89b0148aa2cc", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodDelete)

fmt.Fprintf(w, `{
"status": {
"state": "DELETE_PENDING",
"execution_context": {
"task_uuid": "ff1b9547-dc9a-4ebd-a2ff-f2b718af935e"
}
},
"spec": "",
"api_version": "3.1",
"metadata": {
"kind": "projects",
"categories": {
"Project": "default"
}
}
}`)
})

type fields struct {
Expand Down Expand Up @@ -3343,7 +3360,7 @@ func TestOperations_DeleteProject(t *testing.T) {
op := Operations{
client: tt.fields.client,
}
if err := op.DeleteProject(tt.args.UUID); (err != nil) != tt.wantErr {
if _, err := op.DeleteProject(tt.args.UUID); (err != nil) != tt.wantErr {
t.Errorf("Operations.DeleteProject() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
40 changes: 37 additions & 3 deletions nutanix/resource_nutanix_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,56 @@ func resourceNutanixProjectUpdate(d *schema.ResourceData, meta interface{}) erro
project.APIVersion = d.Get("api_version").(string)
}

_, err = conn.V3.UpdateProject(d.Id(), project)
resp, err := conn.V3.UpdateProject(d.Id(), project)
if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
d.SetId("")
}
return err
}

taskUUID := resp.Status.ExecutionContext.TaskUUID.(string)

// Wait for the Image to be available
stateConf := &resource.StateChangeConf{
Pending: []string{"QUEUED", "RUNNING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, taskUUID),
Timeout: subnetTimeout,
Delay: subnetDelay,
MinTimeout: subnetMinTimeout,
}

if _, err := stateConf.WaitForState(); err != nil {
d.SetId("")
return fmt.Errorf("error waiting for project (%s) to update: %s", d.Id(), err)
}

return resourceNutanixProjectRead(d, meta)
}

func resourceNutanixProjectDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API
if err := conn.V3.DeleteProject(d.Id()); err != nil {
return err
resp, err := conn.V3.DeleteProject(d.Id())
if err != nil {
return fmt.Errorf("error deleting project id %s): %s", d.Id(), err)
}

// Wait for the Project to be available
stateConf := &resource.StateChangeConf{
Pending: []string{"QUEUED", "RUNNING", "DELETED_PENDING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, cast.ToString(resp.Status.ExecutionContext.TaskUUID)),
Timeout: subnetTimeout,
Delay: subnetDelay,
MinTimeout: subnetMinTimeout,
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("error waiting for project (%s) to update: %s", d.Id(), err)
}

d.SetId("")
return nil
}

Expand Down

0 comments on commit 927d91f

Please sign in to comment.