Skip to content

Commit

Permalink
Feat: add Support for K8s Credentials (#847)
Browse files Browse the repository at this point in the history
* Feat: add Support for K8s Credentials

* add azure aks

* added gcp gke

* add update operation
  • Loading branch information
TomerHeber committed May 7, 2024
1 parent d3efb20 commit 7595cfd
Show file tree
Hide file tree
Showing 14 changed files with 1,043 additions and 23 deletions.
1 change: 1 addition & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ type ApiClientInterface interface {
TeamRoleAssignmentDelete(payload *TeamRoleAssignmentDeletePayload) error
TeamRoleAssignments(payload *TeamRoleAssignmentListPayload) ([]TeamRoleAssignmentPayload, error)
KubernetesCredentialsCreate(payload *KubernetesCredentialsCreatePayload) (*Credentials, error)
KubernetesCredentialsUpdate(id string, payload *KubernetesCredentialsUpdatePayload) (*Credentials, error)
}

func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface {
Expand Down
15 changes: 15 additions & 0 deletions client/api_client_mock.go

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

14 changes: 14 additions & 0 deletions client/kubernetes_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type KubernetesCredentialsCreatePayload struct {
Value interface{} `json:"value"`
}

type KubernetesCredentialsUpdatePayload struct {
Type KubernetesCrednetialsType `json:"type"`
Value interface{} `json:"value"`
}

// K8S_KUBECONFIG_FILE
type KubeconfigFileValue struct {
KubeConfig string `json:"kubeConfig"`
Expand Down Expand Up @@ -63,3 +68,12 @@ func (client *ApiClient) KubernetesCredentialsCreate(payload *KubernetesCredenti

return &result, nil
}

func (client *ApiClient) KubernetesCredentialsUpdate(id string, payload *KubernetesCredentialsUpdatePayload) (*Credentials, error) {
var result Credentials
if err := client.http.Patch("/credentials/"+id, payload, &result); err != nil {
return nil, err
}

return &result, nil
}
43 changes: 34 additions & 9 deletions client/kubernetes_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
var _ = Describe("Kubernetes Credentials", func() {
var credentials *Credentials

Describe("KubernetesCredentialsCreate", func() {
value := AzureAksValue{
ClusterName: "cc11",
ResourceGroup: "rg11",
}
mockCredentials := Credentials{
Id: "id111",
}

value := AzureAksValue{
ClusterName: "cc11",
ResourceGroup: "rg11",
}

Describe("KubernetesCredentialsCreate", func() {
createPayload := KubernetesCredentialsCreatePayload{
Name: "n1",
Type: "K8S_AZURE_AKS_AUTH",
Expand All @@ -34,10 +38,6 @@ var _ = Describe("Kubernetes Credentials", func() {
Value: createPayload.Value,
}

mockCredentials := Credentials{
Id: "id111",
}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

Expand All @@ -62,4 +62,29 @@ var _ = Describe("Kubernetes Credentials", func() {
Expect(credentials).To(Equal(&mockCredentials))
})
})

Describe("KubernetesCredentialsUpdate", func() {
updatePayload := KubernetesCredentialsUpdatePayload{
Type: "K8S_AZURE_AKS_AUTH",
Value: value,
}

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Patch("/credentials/"+mockCredentials.Id, &updatePayload, gomock.Any()).
Do(func(path string, request interface{}, response *Credentials) {
*response = mockCredentials
})

credentials, _ = apiClient.KubernetesCredentialsUpdate(mockCredentials.Id, &updatePayload)
})

It("Should send PATCH request with params", func() {
httpCall.Times(1)
})

It("Should return key", func() {
Expect(credentials).To(Equal(&mockCredentials))
})
})
})
3 changes: 3 additions & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func Provider(version string) plugin.ProviderFunc {
"env0_project_budget": resourceProjectBudget(),
"env0_environment_discovery_configuration": resourceEnvironmentDiscoveryConfiguration(),
"env0_kubeconfig_credentials": resourceKubeconfigCredentials(),
"env0_aws_eks_credentials": resourceAwsEksCredentials(),
"env0_azure_aks_credentials": resourceAzureAksCredentials(),
"env0_gcp_gke_credentials": resourceGcpGkeCredentials(),
},
}

Expand Down
83 changes: 83 additions & 0 deletions env0/resource_aws_eks_credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package env0

import (
"context"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceAwsEksCredentials() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAwsEksCredentialsCreate,
UpdateContext: resourceAwsEksCredentialsUpdate,
ReadContext: resourceCredentialsRead(AWS_EKS_TYPE),
DeleteContext: resourceCredentialsDelete,

Importer: &schema.ResourceImporter{StateContext: resourceCredentialsImport(AWS_EKS_TYPE)},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "name for the credentials",
Required: true,
ForceNew: true,
},
"cluster_name": {
Type: schema.TypeString,
Description: "eks cluster name",
Required: true,
},
"cluster_region": {
Type: schema.TypeString,
Description: "the AWS region of the eks cluster",
Required: true,
},
},
}
}

func resourceAwsEksCredentialsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
value := client.AwsEksValue{}
if err := readResourceData(&value, d); err != nil {
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

apiClient := meta.(client.ApiClientInterface)

request := client.KubernetesCredentialsCreatePayload{
Name: d.Get("name").(string),
Value: value,
Type: client.AwsEksCredentialsType,
}

credentials, err := apiClient.KubernetesCredentialsCreate(&request)
if err != nil {
return diag.Errorf("could not create credentials: %v", err)
}

d.SetId(credentials.Id)

return nil
}

func resourceAwsEksCredentialsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
value := client.AwsEksValue{}
if err := readResourceData(&value, d); err != nil {
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

apiClient := meta.(client.ApiClientInterface)

request := client.KubernetesCredentialsUpdatePayload{
Value: value,
Type: client.AwsEksCredentialsType,
}

if _, err := apiClient.KubernetesCredentialsUpdate(d.Id(), &request); err != nil {
return diag.Errorf("could not create credentials: %v", err)
}

return nil
}
Loading

0 comments on commit 7595cfd

Please sign in to comment.