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

refactor: implement workaround for id replacement queries #74

Merged
merged 1 commit into from
Oct 15, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query (
$id: Int!) {
organizationByName(
id: $id) {
projects {
id
name
organization
groupCount
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query (
$name: String!) {
projectByName(
name: $name) {
id
name
organization
}
}
16 changes: 16 additions & 0 deletions api/lagoon/client/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"

"github.com/uselagoon/machinery/api/schema"
)
Expand All @@ -23,6 +24,21 @@ func (c *Client) DeploymentsByBulkID(
})
}

// DeploymentsByEnvironmentAndProjectName is the same as DeploymentsByEnvironment but does a project lookup before the deployments call
func (c *Client) DeploymentsByEnvironmentAndProjectName(
ctx context.Context, projectName, environmentName string, environment *schema.Environment) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.DeploymentsByEnvironment(ctx, project.ID, environmentName, environment)
}

func (c *Client) DeploymentsByEnvironment(
ctx context.Context, projectID uint, environmentName string, environment *schema.Environment) error {
req, err := c.newRequest("_lgraphql/deployments/getDeploymentsForEnvironment.graphql",
Expand Down
52 changes: 50 additions & 2 deletions api/lagoon/client/deploytargetconfigs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package client

import (
"context"
"fmt"

"github.com/uselagoon/machinery/api/schema"
)

// DeployTargetConfigsByProjectID queries the Lagoon API for a projects deploytarget configs by its id, and
// unmarshals the response into deploytargetconfigs.
func (c *Client) DeployTargetConfigsByProjectID(
ctx context.Context, project int, deploytargetconfigs *[]schema.DeployTargetConfig) error {
ctx context.Context, project uint, deploytargetconfigs *[]schema.DeployTargetConfig) error {
req, err := c.newRequest("_lgraphql/deploytargetconfigs/deployTargetConfigsByProjectId.graphql",
map[string]interface{}{
"project": project,
Expand All @@ -25,6 +26,21 @@ func (c *Client) DeployTargetConfigsByProjectID(
})
}

// DeployTargetConfigsByProjectName is the same as DeployTargetConfigsByProjectID but does a project lookup before the deployments call
func (c *Client) DeployTargetConfigsByProjectName(
ctx context.Context, projectName string, deploytargetconfigs *[]schema.DeployTargetConfig) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.DeployTargetConfigsByProjectID(ctx, project.ID, deploytargetconfigs)
}

// AddDeployTargetConfiguration adds a deploytarget configuration to a project.
func (c *Client) AddDeployTargetConfiguration(ctx context.Context,
in *schema.AddDeployTargetConfigInput, out *schema.DeployTargetConfig) error {
Expand All @@ -39,6 +55,23 @@ func (c *Client) AddDeployTargetConfiguration(ctx context.Context,
})
}

// AddDeployTargetConfiguration adds a deploytarget configuration to a project.
func (c *Client) AddDeployTargetConfigurationByProjectName(ctx context.Context,
projectName string,
in *schema.AddDeployTargetConfigInput, out *schema.DeployTargetConfig) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
in.Project = project.ID
return c.AddDeployTargetConfiguration(ctx, in, out)
}

// UpdateDeployTargetConfiguration adds a deploytarget configuration to a project.
func (c *Client) UpdateDeployTargetConfiguration(ctx context.Context,
in *schema.UpdateDeployTargetConfigInput, out *schema.DeployTargetConfig) error {
Expand All @@ -55,7 +88,7 @@ func (c *Client) UpdateDeployTargetConfiguration(ctx context.Context,

// DeleteDeployTargetConfig deletes a deploytarget config from a project.
func (c *Client) DeleteDeployTargetConfiguration(ctx context.Context,
id int, project int, out *schema.DeleteDeployTargetConfig) error {
id, project uint, out *schema.DeleteDeployTargetConfig) error {
req, err := c.newRequest("_lgraphql/deploytargetconfigs/deleteDeployTargetConfig.graphql",
map[string]interface{}{
"id": id,
Expand All @@ -66,3 +99,18 @@ func (c *Client) DeleteDeployTargetConfiguration(ctx context.Context,
}
return c.client.Run(ctx, req, &out)
}

// DeleteDeployTargetConfigurationByIDAndProjectName is the same as DeleteDeployTargetConfig but does a project lookup before the deployments call
func (c *Client) DeleteDeployTargetConfigurationByIDAndProjectName(ctx context.Context,
id uint, projectName string, out *schema.DeleteDeployTargetConfig) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.DeleteDeployTargetConfiguration(ctx, id, project.ID, out)
}
29 changes: 29 additions & 0 deletions api/lagoon/client/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"encoding/json"
"fmt"

"github.com/uselagoon/machinery/api/schema"
)
Expand Down Expand Up @@ -71,6 +72,20 @@ func (c *Client) EnvironmentByName(ctx context.Context, name string,
})
}

func (c *Client) EnvironmentByNameAndProjectName(ctx context.Context, name string,
projectName string, environment *schema.Environment) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.EnvironmentByName(ctx, name, project.ID, environment)
}

// EnvironmentByID queries the Lagoon API for an environment by its ID and unmarshals the response into environment.
func (c *Client) EnvironmentByID(ctx context.Context, environmentID uint, environment *schema.Environment) error {

Expand Down Expand Up @@ -177,6 +192,20 @@ func (c *Client) BackupsForEnvironmentByName(ctx context.Context, name string,
})
}

func (c *Client) BackupsForEnvironmentByNameAndProjectName(ctx context.Context, name string,
projectName string, environment *schema.Environment) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.BackupsForEnvironmentByName(ctx, name, project.ID, environment)
}

// AddRestore adds a restore.
func (c *Client) AddRestore(
ctx context.Context, backupID string, out *schema.Restore) error {
Expand Down
96 changes: 93 additions & 3 deletions api/lagoon/client/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package client
import (
"context"
"encoding/json"
"fmt"

"github.com/uselagoon/machinery/api/schema"
)

Expand Down Expand Up @@ -66,6 +68,25 @@ func (c *Client) MinimalProjectByName(
})
}

// VeryMinimalProjectByName queries the Lagoon API for a very minimal project by its name, and
// unmarshals the response into project.
// only to be used internally for requests to use by name instead of id
func (c *Client) veryMinimalProjectByName(
ctx context.Context, name string, project *schema.Project) error {
req, err := c.newRequest("_lgraphql/projects/veryMinimalProjectByName.graphql",
map[string]interface{}{
"name": name,
})
if err != nil {
return err
}
return c.client.Run(ctx, req, &struct {
Response *schema.Project `json:"projectByName"`
}{
Response: project,
})
}

// ProjectByNameMetadata queries the Lagoon API for a project by its name, and
// unmarshals the response into project.
func (c *Client) ProjectByNameMetadata(
Expand Down Expand Up @@ -123,7 +144,7 @@ func (c *Client) AddProject(

// UpdateProjectMetadata updates a projects metadata.
func (c *Client) UpdateProjectMetadata(
ctx context.Context, id int, key string, value string, projects *schema.ProjectMetadata) error {
ctx context.Context, id uint, key string, value string, projects *schema.ProjectMetadata) error {

req, err := c.newRequest("_lgraphql/projects/updateProjectMetadata.graphql",
map[string]interface{}{
Expand All @@ -142,9 +163,23 @@ func (c *Client) UpdateProjectMetadata(
})
}

func (c *Client) UpdateProjectMetadataByName(
ctx context.Context, name, key, value string, project *schema.ProjectMetadata) error {
proj := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, name, proj); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.UpdateProjectMetadata(ctx, proj.ID, key, value, project)
}

// RemoveProjectMetadataByKey removes metadata from a project for given key.
func (c *Client) RemoveProjectMetadataByKey(
ctx context.Context, id int, key string, projects *schema.ProjectMetadata) error {
ctx context.Context, id uint, key string, projects *schema.ProjectMetadata) error {

req, err := c.newRequest("_lgraphql/projects/removeProjectMetadataByKey.graphql",
map[string]interface{}{
Expand All @@ -162,9 +197,23 @@ func (c *Client) RemoveProjectMetadataByKey(
})
}

func (c *Client) RemoveProjectMetadataByKeyByName(
ctx context.Context, name, key string, project *schema.ProjectMetadata) error {
proj := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, name, proj); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.RemoveProjectMetadataByKey(ctx, proj.ID, key, project)
}

// UpdateProject updates a project.
func (c *Client) UpdateProject(
ctx context.Context, id int, patch schema.UpdateProjectPatchInput, projects *schema.Project) error {
ctx context.Context, id uint, patch schema.UpdateProjectPatchInput, projects *schema.Project) error {

req, err := c.newRequest("_lgraphql/projects/updateProject.graphql",
map[string]interface{}{
Expand All @@ -182,6 +231,20 @@ func (c *Client) UpdateProject(
})
}

func (c *Client) UpdateProjectByName(
ctx context.Context, name string, patch schema.UpdateProjectPatchInput, project *schema.Project) error {
proj := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, name, proj); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.UpdateProject(ctx, proj.ID, patch, project)
}

// ProjectGroups queries the Lagoon API for a project by its name, returning all groups within the project.
func (c *Client) ProjectGroups(
ctx context.Context, name string, project *schema.Project) error {
Expand Down Expand Up @@ -230,6 +293,33 @@ func (c *Client) ProjectsByOrganizationID(ctx context.Context, id uint, projects
return nil
}

// ProjectsByOrganizationName queries the Lagoon API for projects by the given organization name
// and unmarshals the response into organization.
func (c *Client) ProjectsByOrganizationName(ctx context.Context, name string, projects *[]schema.OrgProject) error {
req, err := c.newRequest("_lgraphql/projects/projectsByOrganizationName.graphql",
map[string]interface{}{
"name": name,
})
if err != nil {
return err
}
o := &schema.Organization{}
err = c.client.Run(ctx, req, &struct {
Response *schema.Organization `json:"organizationByName"`
}{
Response: o,
})
if err != nil {
return err
}
data, err := json.Marshal(o.Projects)
if err != nil {
return err
}
json.Unmarshal(data, projects)
return nil
}

// RemoveProjectFromOrganization removes a project from an organization.
func (c *Client) RemoveProjectFromOrganization(ctx context.Context, in *schema.RemoveProjectFromOrganizationInput, out *schema.Project) error {
req, err := c.newRequest("_lgraphql/projects/removeProjectFromOrganization.graphql", in)
Expand Down
30 changes: 30 additions & 0 deletions api/lagoon/client/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ func (c *Client) TasksByEnvironment(ctx context.Context, projectID uint, environ
})
}

// TasksByEnvironmentAndProjectName is the same as TasksByEnvironment but does a project lookup first
func (c *Client) TasksByEnvironmentAndProjectName(ctx context.Context, name string,
projectName string, environment *schema.Environment) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.TasksByEnvironment(ctx, project.ID, name, environment)
}

// InvokableAdvancedTaskDefinitionsByEnvironment gets tasks for an environment.
func (c *Client) InvokableAdvancedTaskDefinitionsByEnvironment(ctx context.Context, projectID uint, environmentName string, environment *schema.Environment) error {
req, err := c.newRequest("_lgraphql/tasks/getInvokableAdvancedTaskDefinitionsByEnvironment.graphql",
Expand All @@ -185,6 +200,21 @@ func (c *Client) InvokableAdvancedTaskDefinitionsByEnvironment(ctx context.Conte
})
}

// InvokableAdvancedTaskDefinitionsByEnvironmentAndProjectName is the same as InvokableAdvancedTaskDefinitionsByEnvironment but does a project lookup first
func (c *Client) InvokableAdvancedTaskDefinitionsByEnvironmentAndProjectName(ctx context.Context, name string,
projectName string, environment *schema.Environment) error {
project := &schema.Project{}
if err := c.veryMinimalProjectByName(ctx, projectName, project); err != nil {
return err
}
if project.Name == "" {
//lint:ignore ST1005 return a generic Lagoon API unauthorized error based on the permission called
// this is because organizationbyname will return null instead of an error, the api should probably return an error
return fmt.Errorf(`Unauthorized: You don't have permission to "view" on "project"`)
}
return c.InvokableAdvancedTaskDefinitionsByEnvironment(ctx, project.ID, name, environment)
}

// InvokeAdvancedTaskDefinition invokes an advanced task definition.
func (c *Client) InvokeAdvancedTaskDefinition(ctx context.Context, environmentID uint, taskID uint, task *schema.Task) error {
req, err := c.newRequest("_lgraphql/tasks/invokeRegisteredTask.graphql",
Expand Down
Loading