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

feat(CLOUDDEV-857): add SCHEDULED_FOR_DELETION state and corresponding methods #69

Merged
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
66 changes: 54 additions & 12 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
)

const (
projectsBasePath = "/v1/projects"
projectsBasePath = "/v1/projects"
projectsScheduleDeletion = "schedule_deletion"
projectsCancelScheduledDeletion = "cancel_scheduled_deletion"
)

// ProjectsService is an interface for creating and managing Projects with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/projects
type ProjectsService interface {
Get(context.Context, string) (*Project, *Response, error)
ScheduleDeletion(context.Context, string) (*Project, *Response, error)
CancelScheduledDeletion(context.Context, string) (*Project, *Response, error)
Delete(context.Context, string) (*TaskResponse, *Response, error)
Update(context.Context, string, *ProjectUpdateRequest) (*Project, *Response, error)
List(context.Context, *ProjectListOptions) ([]Project, *Response, error)
Expand All @@ -32,21 +36,23 @@ type ProjectState string

// List of ProjectState.
const (
ProjectStateActive ProjectState = "ACTIVE"
ProjectStateDeleted ProjectState = "DELETED"
ProjectStateDeleting ProjectState = "DELETING"
ProjectStateActive ProjectState = "ACTIVE"
ProjectStateScheduledForDeletion ProjectState = "SCHEDULED_FOR_DELETION"
ProjectStateDeleted ProjectState = "DELETED"
ProjectStateDeleting ProjectState = "DELETING"
)

// Project represents a EdgecenterCloud Project configuration.
type Project struct {
ID int `json:"id"`
ClientID int `json:"client_id"`
CreatedAt string `json:"created_at"`
Description string `json:"description"`
IsDefault bool `json:"is_default"`
Name string `json:"name"`
State ProjectState `json:"state"`
TaskID *string `json:"task_id"`
ID int `json:"id"`
ClientID int `json:"client_id"`
CreatedAt string `json:"created_at"`
ScheduledForDeletionAt *string `json:"scheduled_for_deletion_at"`
Description string `json:"description"`
IsDefault bool `json:"is_default"`
Name string `json:"name"`
State ProjectState `json:"state"`
TaskID *string `json:"task_id"`
}

// ProjectUpdateRequest represents a request to update a Project.
Expand Down Expand Up @@ -93,6 +99,42 @@ func (s *ProjectsServiceOp) Get(ctx context.Context, projectID string) (*Project
return project, resp, err
}

// ScheduleDeletion schedule project deletion.
func (s *ProjectsServiceOp) ScheduleDeletion(ctx context.Context, projectID string) (*Project, *Response, error) {
path := fmt.Sprintf("%s/%s/%s", projectsBasePath, projectID, projectsScheduleDeletion)

req, err := s.client.NewRequest(ctx, http.MethodPatch, path, nil)
if err != nil {
return nil, nil, err
}

project := new(Project)
resp, err := s.client.Do(ctx, req, project)
if err != nil {
return nil, resp, err
}

return project, resp, err
}

// CancelScheduledDeletion cancel scheduled project deletion.
func (s *ProjectsServiceOp) CancelScheduledDeletion(ctx context.Context, projectID string) (*Project, *Response, error) {
path := fmt.Sprintf("%s/%s/%s", projectsBasePath, projectID, projectsCancelScheduledDeletion)

req, err := s.client.NewRequest(ctx, http.MethodPatch, path, nil)
if err != nil {
return nil, nil, err
}

project := new(Project)
resp, err := s.client.Do(ctx, req, project)
if err != nil {
return nil, resp, err
}

return project, resp, err
}

// Delete a project.
func (s *ProjectsServiceOp) Delete(ctx context.Context, projectID string) (*TaskResponse, *Response, error) {
path := fmt.Sprintf("%s/%s", projectsBasePath, projectID)
Expand Down
55 changes: 55 additions & 0 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,61 @@ func TestProjects_Get(t *testing.T) {
require.Equal(t, respActual, expectedResp)
}

func TestProjects_ScheduleDeletion(t *testing.T) {
setup()
defer teardown()

scheduledForDeletionAt := "2024-08-06T17:40:32"
expectedResp := &Project{
ID: 1,
Name: "project-scheduled-for-deletion",
State: ProjectStateScheduledForDeletion,
ScheduledForDeletionAt: &scheduledForDeletionAt,
}
URL := path.Join(projectsBasePath, "1", projectsScheduleDeletion)

mux.HandleFunc(URL, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPatch)
resp, err := json.Marshal(expectedResp)
if err != nil {
t.Errorf("failed to marshal response: %v", err)
}
_, _ = fmt.Fprint(w, string(resp))
})

respActual, resp, err := client.Projects.ScheduleDeletion(ctx, "1")
require.NoError(t, err)
require.Equal(t, resp.StatusCode, 200)
require.Equal(t, respActual, expectedResp)
}

func TestProjects_CancelScheduledDeletion(t *testing.T) {
setup()
defer teardown()

expectedResp := &Project{
ID: 1,
Name: "active-project",
State: ProjectStateActive,
ScheduledForDeletionAt: nil,
}
URL := path.Join(projectsBasePath, "1", projectsScheduleDeletion)

mux.HandleFunc(URL, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPatch)
resp, err := json.Marshal(expectedResp)
if err != nil {
t.Errorf("failed to marshal response: %v", err)
}
_, _ = fmt.Fprint(w, string(resp))
})

respActual, resp, err := client.Projects.ScheduleDeletion(ctx, "1")
require.NoError(t, err)
require.Equal(t, resp.StatusCode, 200)
require.Equal(t, respActual, expectedResp)
}

func TestProjects_Delete(t *testing.T) {
setup()
defer teardown()
Expand Down
Loading