Repository: xanzy/go-gitlab
Pull request #2013 (2024-09-19 09:25:10)

Support Kubernetes-related Environments API fields
These fields have been added to 17.4, see
https://docs.gitlab.com/ee/api/environments.html#create-a-new-environment


Patch:
diff --git a/environments.go b/environments.go
index b6d902f..b99975a 100644
--- a/environments.go
+++ b/environments.go
@@ -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 {
@@ -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
@@ -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..


Test patch:
diff --git a/environments_test.go b/environments_test.go
index 717ac8f..0180e81 100644
--- a/environments_test.go
+++ b/environments_test.go
@@ -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"
 			}
 		]`)
 	})
@@ -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)
@@ -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"
 		}`)
 	})
 
@@ -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)
@@ -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)
 	}
@@ -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)
 	}