Skip to content

Commit

Permalink
CLOUDP-228593: Atlas project controller with translation layer and st…
Browse files Browse the repository at this point in the history
…ate machine (#1685)
  • Loading branch information
helderjs authored Jul 26, 2024
1 parent 497cee3 commit 878249d
Show file tree
Hide file tree
Showing 14 changed files with 2,168 additions and 722 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mockname: "{{.InterfaceName}}Mock"
all: true
packages:
github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/ipaccesslist:
github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/project:
github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/dbuser:
github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/deployment:
github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/customroles:
191 changes: 191 additions & 0 deletions internal/mocks/translation/project_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions internal/translation/project/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package project

import (
"go.mongodb.org/atlas-sdk/v20231115008/admin"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/tag"
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
)

type Project struct {
OrgID string
ID string
Name string
RegionUsageRestrictions string
WithDefaultAlertsSettings bool
Tags []tag.Tag
}

func NewProject(project *akov2.AtlasProject, orgID string) *Project {
return &Project{
OrgID: orgID,
ID: project.ID(),
Name: project.Spec.Name,
RegionUsageRestrictions: project.Spec.RegionUsageRestrictions,
WithDefaultAlertsSettings: project.Spec.WithDefaultAlertsSettings,
}
}

func fromAtlas(group *admin.Group) *Project {
return &Project{
OrgID: group.GetOrgId(),
ID: group.GetId(),
Name: group.GetName(),
RegionUsageRestrictions: group.GetRegionUsageRestrictions(),
WithDefaultAlertsSettings: group.GetWithDefaultAlertsSettings(),
Tags: tag.FromAtlas(group.GetTags()),
}
}

func toAtlas(project *Project) *admin.Group {
return &admin.Group{
OrgId: project.OrgID,
Name: project.Name,
RegionUsageRestrictions: &project.RegionUsageRestrictions,
Tags: tag.ToAtlas(project.Tags),
WithDefaultAlertsSettings: &project.WithDefaultAlertsSettings,
}
}
61 changes: 61 additions & 0 deletions internal/translation/project/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package project

import (
"context"

"go.mongodb.org/atlas-sdk/v20231115008/admin"
)

type ProjectService interface {
GetProjectByName(ctx context.Context, name string) (*Project, error)
CreateProject(ctx context.Context, project *Project) error
DeleteProject(ctx context.Context, project *Project) error
}

type ProjectAPI struct {
projectAPI admin.ProjectsApi
}

func (a *ProjectAPI) GetProjectByName(ctx context.Context, name string) (*Project, error) {
group, _, err := a.projectAPI.GetProjectByName(ctx, name).Execute()
if err != nil {
if admin.IsErrorCode(err, "NOT_IN_GROUP") || admin.IsErrorCode(err, "RESOURCE_NOT_FOUND") {
return nil, nil
}

return nil, err
}

return fromAtlas(group), err
}

func (a *ProjectAPI) CreateProject(ctx context.Context, project *Project) error {
group, _, err := a.projectAPI.CreateProject(ctx, toAtlas(project)).Execute()
if err != nil {
return err
}

project.OrgID = group.GetOrgId()
project.ID = group.GetId()

return nil
}

func (a *ProjectAPI) DeleteProject(ctx context.Context, project *Project) error {
_, _, err := a.projectAPI.DeleteProject(ctx, project.ID).Execute()
if err != nil {
if admin.IsErrorCode(err, "GROUP_NOT_FOUND") || admin.IsErrorCode(err, "RESOURCE_NOT_FOUND") {
return nil
}

return err
}

return nil
}

func NewProjectAPIService(sdk admin.ProjectsApi) *ProjectAPI {
return &ProjectAPI{
projectAPI: sdk,
}
}
Loading

0 comments on commit 878249d

Please sign in to comment.