Skip to content

Commit

Permalink
Support Kubernetes-related Environments API fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo Furrer committed Sep 19, 2024
1 parent b8ae589 commit 334d322
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 24 deletions.
41 changes: 25 additions & 16 deletions environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ type EnvironmentsService struct {
//
// GitLab API docs: https://docs.gitlab.com/ee/api/environments.html
type Environment struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
State string `json:"state"`
Tier string `json:"tier"`
ExternalURL string `json:"external_url"`
Project *Project `json:"project"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
LastDeployment *Deployment `json:"last_deployment"`
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
State string `json:"state"`
Tier string `json:"tier"`
ExternalURL string `json:"external_url"`
Project *Project `json:"project"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
LastDeployment *Deployment `json:"last_deployment"`
ClusterAgent *Agent `json:"cluster_agent"`
KubernetesNamespace string `json:"kubernetes_namespace"`
FluxResourcePath string `json:"flux_resource_path"`
}

func (env Environment) String() string {
Expand Down Expand Up @@ -117,9 +120,12 @@ func (s *EnvironmentsService) GetEnvironment(pid interface{}, environment int, o
// GitLab API docs:
// https://docs.gitlab.com/ee/api/environments.html#create-a-new-environment
type CreateEnvironmentOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
Tier *string `url:"tier,omitempty" json:"tier,omitempty"`
Name *string `url:"name,omitempty" json:"name,omitempty"`
ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
Tier *string `url:"tier,omitempty" json:"tier,omitempty"`
ClusterAgentID *int `url:"cluster_agent_id,omitempty" json:"cluster_agent_id,omitempty"`
KubernetesNamespace *string `url:"kubernetes_namespace,omitempty" json:"kubernetes_namespace,omitempty"`
FluxResourcePath *string `url:"flux_resource_path,omitempty" json:"flux_resource_path,omitempty"`
}

// CreateEnvironment adds an environment to a project. This is an idempotent
Expand Down Expand Up @@ -155,9 +161,12 @@ func (s *EnvironmentsService) CreateEnvironment(pid interface{}, opt *CreateEnvi
// GitLab API docs:
// https://docs.gitlab.com/ee/api/environments.html#update-an-existing-environment
type EditEnvironmentOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
Tier *string `url:"tier,omitempty" json:"tier,omitempty"`
Name *string `url:"name,omitempty" json:"name,omitempty"`
ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
Tier *string `url:"tier,omitempty" json:"tier,omitempty"`
ClusterAgentID *int `url:"cluster_agent_id,omitempty" json:"cluster_agent_id,omitempty"`
KubernetesNamespace *string `url:"kubernetes_namespace,omitempty" json:"kubernetes_namespace,omitempty"`
FluxResourcePath *string `url:"flux_resource_path,omitempty" json:"flux_resource_path,omitempty"`
}

// EditEnvironment updates a project team environment to a specified access level..
Expand Down
176 changes: 168 additions & 8 deletions environments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,24 @@ func TestListEnvironments(t *testing.T) {
"external_url": "https://review-fix-foo-dfjre3.example.gitlab.com",
"state": "stopped",
"created_at": "2013-10-02T10:12:29Z",
"updated_at": "2013-12-02T10:12:29Z"
"updated_at": "2013-12-02T10:12:29Z",
"cluster_agent": {
"id": 1,
"name": "agent-1",
"config_project": {
"id": 20,
"description": "",
"name": "test",
"name_with_namespace": "Administrator / test",
"path": "test",
"path_with_namespace": "root/test",
"created_at": "2013-10-02T10:12:29Z"
},
"created_at": "2013-10-02T10:12:29Z",
"created_by_user_id": 42
},
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}
]`)
})
Expand All @@ -62,6 +79,22 @@ func TestListEnvironments(t *testing.T) {
State: "stopped",
CreatedAt: &createdAtWant,
UpdatedAt: &updatedAtWant,
ClusterAgent: &Agent{
ID: 1,
Name: "agent-1",
ConfigProject: ConfigProject{
ID: 20,
Name: "test",
NameWithNamespace: "Administrator / test",
Path: "test",
PathWithNamespace: "root/test",
CreatedAt: &createdAtWant,
},
CreatedAt: &createdAtWant,
CreatedByUserID: 42,
},
KubernetesNamespace: "flux-system",
FluxResourcePath: "HelmRelease/flux-system",
}}
if !reflect.DeepEqual(want, envs) {
t.Errorf("Environments.ListEnvironments returned %+v, want %+v", envs, want)
Expand All @@ -80,7 +113,24 @@ func TestGetEnvironment(t *testing.T) {
"external_url": "https://review-fix-foo-dfjre3.example.gitlab.com",
"state": "stopped",
"created_at": "2013-10-02T10:12:29Z",
"updated_at": "2013-12-02T10:12:29Z"
"updated_at": "2013-12-02T10:12:29Z",
"cluster_agent": {
"id": 1,
"name": "agent-1",
"config_project": {
"id": 20,
"description": "",
"name": "test",
"name_with_namespace": "Administrator / test",
"path": "test",
"path_with_namespace": "root/test",
"created_at": "2013-10-02T10:12:29Z"
},
"created_at": "2013-10-02T10:12:29Z",
"created_by_user_id": 42
},
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}`)
})

Expand All @@ -99,6 +149,22 @@ func TestGetEnvironment(t *testing.T) {
State: "stopped",
CreatedAt: &createdAtWant,
UpdatedAt: &updatedAtWant,
ClusterAgent: &Agent{
ID: 1,
Name: "agent-1",
ConfigProject: ConfigProject{
ID: 20,
Name: "test",
NameWithNamespace: "Administrator / test",
Path: "test",
PathWithNamespace: "root/test",
CreatedAt: &createdAtWant,
},
CreatedAt: &createdAtWant,
CreatedByUserID: 42,
},
KubernetesNamespace: "flux-system",
FluxResourcePath: "HelmRelease/flux-system",
}
if !reflect.DeepEqual(want, env) {
t.Errorf("Environments.GetEnvironment returned %+v, want %+v", env, want)
Expand All @@ -116,16 +182,63 @@ func TestCreateEnvironment(t *testing.T) {
"name": "deploy",
"slug": "deploy",
"external_url": "https://deploy.example.gitlab.com",
"tier": "production"
"tier": "production",
"cluster_agent": {
"id": 1,
"name": "agent-1",
"config_project": {
"id": 20,
"description": "",
"name": "test",
"name_with_namespace": "Administrator / test",
"path": "test",
"path_with_namespace": "root/test",
"created_at": "2013-10-02T10:12:29Z"
},
"created_at": "2013-10-02T10:12:29Z",
"created_by_user_id": 42
},
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}`)
})

envs, _, err := client.Environments.CreateEnvironment(1, &CreateEnvironmentOptions{Name: Ptr("deploy"), ExternalURL: Ptr("https://deploy.example.gitlab.com"), Tier: Ptr("production")})
envs, _, err := client.Environments.CreateEnvironment(1, &CreateEnvironmentOptions{
Name: Ptr("deploy"),
ExternalURL: Ptr("https://deploy.example.gitlab.com"),
Tier: Ptr("production"),
ClusterAgentID: Ptr(1),
KubernetesNamespace: Ptr("flux-system"),
FluxResourcePath: Ptr("HelmRelease/flux-system"),
})
if err != nil {
log.Fatal(err)
}

want := &Environment{ID: 1, Name: "deploy", Slug: "deploy", ExternalURL: "https://deploy.example.gitlab.com", Tier: "production"}
createdAtWant, _ := time.Parse(timeLayout, "2013-10-02T10:12:29Z")
want := &Environment{
ID: 1,
Name: "deploy",
Slug: "deploy",
ExternalURL: "https://deploy.example.gitlab.com",
Tier: "production",
ClusterAgent: &Agent{
ID: 1,
Name: "agent-1",
ConfigProject: ConfigProject{
ID: 20,
Name: "test",
NameWithNamespace: "Administrator / test",
Path: "test",
PathWithNamespace: "root/test",
CreatedAt: &createdAtWant,
},
CreatedAt: &createdAtWant,
CreatedByUserID: 42,
},
KubernetesNamespace: "flux-system",
FluxResourcePath: "HelmRelease/flux-system",
}
if !reflect.DeepEqual(want, envs) {
t.Errorf("Environments.CreateEnvironment returned %+v, want %+v", envs, want)
}
Expand All @@ -142,16 +255,63 @@ func TestEditEnvironment(t *testing.T) {
"name": "staging",
"slug": "staging",
"external_url": "https://staging.example.gitlab.com",
"tier": "staging"
"tier": "staging",
"cluster_agent": {
"id": 1,
"name": "agent-1",
"config_project": {
"id": 20,
"description": "",
"name": "test",
"name_with_namespace": "Administrator / test",
"path": "test",
"path_with_namespace": "root/test",
"created_at": "2013-10-02T10:12:29Z"
},
"created_at": "2013-10-02T10:12:29Z",
"created_by_user_id": 42
},
"kubernetes_namespace": "flux-system",
"flux_resource_path": "HelmRelease/flux-system"
}`)
})

envs, _, err := client.Environments.EditEnvironment(1, 1, &EditEnvironmentOptions{Name: Ptr("staging"), ExternalURL: Ptr("https://staging.example.gitlab.com"), Tier: Ptr("staging")})
envs, _, err := client.Environments.EditEnvironment(1, 1, &EditEnvironmentOptions{
Name: Ptr("staging"),
ExternalURL: Ptr("https://staging.example.gitlab.com"),
Tier: Ptr("staging"),
ClusterAgentID: Ptr(1),
KubernetesNamespace: Ptr("flux-system"),
FluxResourcePath: Ptr("HelmRelease/flux-system"),
})
if err != nil {
log.Fatal(err)
}

want := &Environment{ID: 1, Name: "staging", Slug: "staging", ExternalURL: "https://staging.example.gitlab.com", Tier: "staging"}
createdAtWant, _ := time.Parse(timeLayout, "2013-10-02T10:12:29Z")
want := &Environment{
ID: 1,
Name: "staging",
Slug: "staging",
ExternalURL: "https://staging.example.gitlab.com",
Tier: "staging",
ClusterAgent: &Agent{
ID: 1,
Name: "agent-1",
ConfigProject: ConfigProject{
ID: 20,
Name: "test",
NameWithNamespace: "Administrator / test",
Path: "test",
PathWithNamespace: "root/test",
CreatedAt: &createdAtWant,
},
CreatedAt: &createdAtWant,
CreatedByUserID: 42,
},
KubernetesNamespace: "flux-system",
FluxResourcePath: "HelmRelease/flux-system",
}
if !reflect.DeepEqual(want, envs) {
t.Errorf("Environments.EditEnvironment returned %+v, want %+v", envs, want)
}
Expand Down

0 comments on commit 334d322

Please sign in to comment.