-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDP-228593: Atlas project controller with translation layer and st…
…ate machine (#1685)
- Loading branch information
Showing
14 changed files
with
2,168 additions
and
722 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
Oops, something went wrong.