Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task completion project #234

Merged
merged 5 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3303,6 +3303,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 @@ -3340,7 +3357,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