Skip to content

Commit

Permalink
add create and delete project
Browse files Browse the repository at this point in the history
  • Loading branch information
haruska committed Mar 27, 2024
1 parent d5b3d2f commit 3a25405
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
83 changes: 83 additions & 0 deletions pinecone/management_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,86 @@ func (c *ManagementClient) FetchProject(ctx context.Context, projectId uuid.UUID

return nil, fmt.Errorf("unexpected response format or empty data")
}

// CreateProject creates a new project in the management API. It sends a request to the
// management plane's CreateProject endpoint with the project details.
//
// Parameters:
// - ctx: A context.Context to control the request's lifetime.
// - projectName: A string representing the name of the project to create.
//
// Returns the created project's details on success or an error if the creation fails.
// Possible errors include unauthorized access, validation errors, internal server errors,
// or other HTTP client errors.
//
// Example:
//
// project, err := managementClient.CreateProject(ctx, "New Project Name")
// if err != nil {
// log.Fatalf("Failed to create project: %v", err)
// }
// fmt.Printf("Created Project ID: %s, Name: %s\n", project.Id, project.Name)
func (c *ManagementClient) CreateProject(ctx context.Context, projectName string) (*Project, error) {
body := management.CreateProjectJSONRequestBody{
Name: projectName,
}

resp, err := c.restClient.CreateProjectWithResponse(ctx, body)
if err != nil {
return nil, fmt.Errorf("failed to create project: %w", err)
}

switch resp.StatusCode() {
case http.StatusCreated:
if resp.JSON201 != nil {
return &Project{
Id: resp.JSON201.Id,
Name: resp.JSON201.Name,
}, nil
}
case http.StatusUnauthorized:
return nil, fmt.Errorf("unauthorized: %v", resp.JSON401)
case http.StatusBadRequest:
return nil, fmt.Errorf("bad request: %v", resp.JSON400)
default:
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}

return nil, fmt.Errorf("unexpected response format or empty data")
}

// DeleteProject deletes a project by its ID from the management API. It makes a call to the
// management plane's DeleteProject endpoint.
//
// Parameters:
// - ctx: A context.Context to control the request's lifetime.
// - projectId: A string representing the unique identifier of the project to delete.
//
// Returns an error if the deletion fails. Possible errors include unauthorized access,
// project not found, internal server errors, or other HTTP client errors.
//
// Example:
//
// err := managementClient.DeleteProject(ctx, "your_project_id_here")
// if err != nil {
// log.Fatalf("Failed to delete project: %v", err)
// }
func (c *ManagementClient) DeleteProject(ctx context.Context, projectId uuid.UUID) error {
resp, err := c.restClient.DeleteProjectWithResponse(ctx, projectId)
if err != nil {
return fmt.Errorf("failed to delete project: %w", err)
}

switch resp.StatusCode() {
case http.StatusOK, http.StatusAccepted, http.StatusNoContent:
return nil // Success case
case http.StatusUnauthorized:
return fmt.Errorf("unauthorized: %v", resp.JSON401)
case http.StatusNotFound:
return fmt.Errorf("project not found: %v", resp.JSON404)
case http.StatusInternalServerError:
return fmt.Errorf("internal server error: %v", resp.JSON500)
default:
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}
}
34 changes: 34 additions & 0 deletions pinecone/management_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package pinecone

import (
"context"
"fmt"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -51,3 +53,35 @@ func (ts *ManagementClientTests) TestFetchProject() {
require.Equal(ts.T(), testProjectID, project.Id, "Fetched project ID should match the requested ID")
require.Equal(ts.T(), ts.project.Name, project.Name, "Fetched project name should match the expected name")
}

func (ts *ManagementClientTests) TestCreateProject() {
projectName := "TestProject_" + fmt.Sprint(time.Now().UnixNano())

createdProject, err := ts.client.CreateProject(context.Background(), projectName)
defer func() {
if createdProject != nil {
delErr := ts.client.DeleteProject(context.Background(), createdProject.Id)
require.NoError(ts.T(), delErr, "Failed to clean up project")
}
}()

require.NoError(ts.T(), err, "Failed to create project")
require.NotNil(ts.T(), createdProject, "Created project should not be nil")
require.Equal(ts.T(), projectName, createdProject.Name, "Created project name should match")
}

func (ts *ManagementClientTests) TestDeleteProject() {
// Create a project to delete
projectName := "TestProjectForDeletion_" + fmt.Sprint(time.Now().UnixNano())
projectToDelete, err := ts.client.CreateProject(context.Background(), projectName)
require.NoError(ts.T(), err, "Failed to create project for deletion")
require.NotNil(ts.T(), projectToDelete, "Project for deletion should not be nil")

// Attempt to delete the project
err = ts.client.DeleteProject(context.Background(), projectToDelete.Id)
require.NoError(ts.T(), err, "Failed to delete project")

// Verify deletion by attempting to fetch the deleted project
_, fetchErr := ts.client.FetchProject(context.Background(), projectToDelete.Id)
require.Error(ts.T(), fetchErr, "Expected an error when fetching a deleted project")
}

0 comments on commit 3a25405

Please sign in to comment.