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

EKS access entry and policy association changes #35037

Merged
merged 40 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
bc7d5bb
EKS access entry and policy association changes
sasidhar-aws Dec 22, 2023
8871281
corrected review comments in documentation
sasidhar-aws Jan 4, 2024
eae825c
updated access entry resource logic
sasidhar-aws Jan 9, 2024
79df630
updated access entry and access policy association resource logic
sasidhar-aws Jan 9, 2024
4518d71
fixed access policy association issues
sasidhar-aws Jan 14, 2024
98b9df4
Merge branch 'main' into HEAD
ewbankkit Jan 17, 2024
0df367b
Add CHANGELOG entries.
ewbankkit Jan 17, 2024
47803d1
Update website/docs/r/eks_access_policy_association.html.markdown
ewbankkit Jan 17, 2024
0c96a69
Update website/docs/r/eks_access_policy_association.html.markdown
ewbankkit Jan 17, 2024
fec00bb
Update website/docs/r/eks_access_policy_association.html.markdown
ewbankkit Jan 17, 2024
cbb150a
Update website/docs/d/eks_cluster.html.markdown
ewbankkit Jan 17, 2024
4f375e1
Update website/docs/r/eks_cluster.html.markdown
ewbankkit Jan 17, 2024
a63c4f0
Update website/docs/r/eks_cluster.html.markdown
ewbankkit Jan 17, 2024
5e8a5c0
Merge commit '0df367b986f59ebf9cce1d2a0e37b80028787a1d' into HEAD
ewbankkit Jan 17, 2024
b3350b4
Fix terrafmt errors in acceptance test configurations.
ewbankkit Jan 17, 2024
7b6377a
Fix tfproviderdocs 'file does not end with a valid extension, valid e…
ewbankkit Jan 17, 2024
7912fb9
Fix terrafmt errors in documentation.
ewbankkit Jan 17, 2024
0429ed3
Fix importlint errors.
ewbankkit Jan 17, 2024
372e573
Fix markdownlint 'MD012/no-multiple-blanks Multiple consecutive blank…
ewbankkit Jan 17, 2024
3254b7d
Fix markdownlint 'MD047/single-trailing-newline Files should end with…
ewbankkit Jan 17, 2024
cc1288d
Fix markdownlint 'MD031/blanks-around-fences Fenced code blocks shoul…
ewbankkit Jan 17, 2024
a2b6793
Run 'make gen'.
ewbankkit Jan 17, 2024
9f3dd2e
Fix providerlint 'AWSAT005: avoid hardcoded ARN AWS partitions, use a…
ewbankkit Jan 17, 2024
671526d
Fix semgrep errors.
ewbankkit Jan 17, 2024
6594cf4
r/aws_eks_cluster: Tidy up.
ewbankkit Jan 17, 2024
9a1f036
d/aws_eks_cluster: Tidy up.
ewbankkit Jan 17, 2024
aebc49a
Fix terrafmt errors in documentation.
ewbankkit Jan 17, 2024
7446fed
Fix markdownlint 'MD047/single-trailing-newline Files should end with…
ewbankkit Jan 17, 2024
63ee061
Fix golangci-lint 'whitespace'.
ewbankkit Jan 17, 2024
0e40def
Fix golangci-lint 'exportloopref'.
ewbankkit Jan 17, 2024
e148633
r/aws_eks_cluster: Remove 'MinItems' from 'access_config'.
ewbankkit Jan 17, 2024
b4501e0
Check 'access_config' in 'TestAccEKSClusterDataSource_basic'.
ewbankkit Jan 17, 2024
e9431c5
r/aws_eks_cluster: Additional 'access_config' checks.
ewbankkit Jan 17, 2024
d3ac1ad
Remove 'access_config' from acceptance test configuration.
ewbankkit Jan 17, 2024
21ff1a8
Fix terrafmt error.
ewbankkit Jan 18, 2024
bced003
r/aws_eks_cluster: 'access_config' is Computed.
ewbankkit Jan 18, 2024
7c19200
Acceptance test output:
ewbankkit Jan 18, 2024
955f816
aws_eks_access_entry: Tidy up and additional tests.
ewbankkit Jan 18, 2024
fb7bef3
eks: Correct 'resourceAccessEntryUpdate'.
ewbankkit Jan 18, 2024
152868a
r/aws_eks_access_policy_association: Tidy up and additional tests.
ewbankkit Jan 18, 2024
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
205 changes: 205 additions & 0 deletions internal/service/eks/access_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package eks

import (
"context"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_eks_access_entry", name="Access Entry")
// @Tags(identifierAttribute="arn")
func ResourceAccessEntry() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAccessEntryCreate,
ReadWithoutTimeout: resourceAccessEntryRead,
UpdateWithoutTimeout: resourceAccessEntryUpdate,
DeleteWithoutTimeout: resourceAccessEntryDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

CustomizeDiff: verify.SetTagsDiff,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"access_entry_arn": {
Type: schema.TypeString,
Computed: true,
},
"cluster_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validClusterName,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"kubernetes_group": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"modified_at": {
Type: schema.TypeString,
Computed: true,
},
"principal_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
}
}

func resourceAccessEntryCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSClient(ctx)

clusterName := d.Get("cluster_name").(string)
principal_arn := d.Get("principal_arn").(string)
accessID := AccessEntryCreateResourceID(clusterName, principal_arn)
input := &eks.CreateAccessEntryInput{
ClusterName: aws.String(clusterName),
PrincipalArn: aws.String(principal_arn),
KubernetesGroups: flex.ExpandStringValueSet(d.Get("kubernetes_groups").(*schema.Set)),
Tags: getTagsIn(ctx),
}

_, err := conn.CreateAccessEntry(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating EKS Access Config: %s", err)
}

d.SetId(accessID)

return append(diags, resourceAccessEntryRead(ctx, d, meta)...)
}

func resourceAccessEntryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSClient(ctx)

clusterName, principal_arn, err := AccessEntryParseResourceID(d.Id())
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EKS Access Entry (%s): %s", d.Id(), err)
}
output, err := FindAccessEntryByID(ctx, conn, clusterName, principal_arn)

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EKS Access Entry (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EKS EKS Access Entry (%s): %s", d.Id(), err)
}

d.Set("access_entry_arn", output.AccessEntryArn)
d.Set("cluster_name", output.ClusterName)
d.Set("created_at", output.CreatedAt)
// if err := d.Set("kubernetes_groups", aws.StringValueSlice(output.KubernetesGroups)); err != nil {
// return sdkdiag.AppendErrorf(diags, "setting kubernetes_groups: %s", err)
// }
d.Set("kubernetes_groups", output.KubernetesGroups)
d.Set("modified_at", output.ModifiedAt)
d.Set("principal_arn", output.PrincipalArn)
d.Set("user_name", output.Username)
d.Set("type", output.Type)

setTagsOut(ctx, output.Tags)

return diags
}

func resourceAccessEntryUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSClient(ctx)
clusterName, principal_arn, err := AccessEntryParseResourceID(d.Id())

if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
if d.HasChangesExcept("tags", "tags_all") {
input := &eks.UpdateAccessEntryInput{
ClusterName: aws.String(clusterName),
PrincipalArn: aws.String(principal_arn),
}

if d.HasChange("kubernetes_group") {
input.KubernetesGroups = flex.ExpandStringValueSet(d.Get("kubernetes_groups").(*schema.Set))
}

_, err := conn.UpdateAccessEntry(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Access Entry (%s): %s", d.Id(), err)
}
}

return append(diags, resourceFargateProfileRead(ctx, d, meta)...)
}

func resourceAccessEntryDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSClient(ctx)

clusterName, principal_arn, err := AccessEntryParseResourceID(d.Id())

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EKS Access Entry (%s): %s", d.Id(), err)
}

log.Printf("[DEBUG] Deleting EKS Access Entry: %s", d.Id())
_, err = conn.DeleteAccessEntry(ctx, &eks.DeleteAccessEntryInput{
ClusterName: aws.String(clusterName),
PrincipalArn: aws.String(principal_arn),
})

if errs.IsAErrorMessageContains[*types.ResourceNotFoundException](err, "No Access Entry found for Id:") {
return nil
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EKS Access Entry (%s): %s", d.Id(), err)
}

return diags
}
104 changes: 104 additions & 0 deletions internal/service/eks/access_entry_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package eks

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKDataSource("aws_eks_access_entry")
func DataSourceAccessEntry() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceAccessEntryRead,

Schema: map[string]*schema.Schema{
"access_entry_arn": {
Type: schema.TypeString,
Computed: true,
},
"cluster_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validClusterName,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"kubernetes_group": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"modified_at": {
Type: schema.TypeString,
Computed: true,
},
"principal_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
}
}

func dataSourceAccessEntryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EKSClient(ctx)

clusterName, principal_arn, err := AccessEntryParseResourceID(d.Id())
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EKS Access Entry (%s): %s", d.Id(), err)
}
output, err := FindAccessEntryByID(ctx, conn, clusterName, principal_arn)

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EKS Access Entry (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EKS EKS Access Entry (%s): %s", d.Id(), err)
}

d.Set("access_entry_arn", output.AccessEntryArn)
d.Set("cluster_name", output.ClusterName)
d.Set("created_at", output.CreatedAt)
// if err := d.Set("kubernetes_groups", aws.StringValueSlice(output.KubernetesGroups)); err != nil {
// return sdkdiag.AppendErrorf(diags, "setting kubernetes_groups: %s", err)
// }
d.Set("kubernetes_groups", output.KubernetesGroups)
d.Set("modified_at", output.ModifiedAt)
d.Set("principal_arn", output.PrincipalArn)
d.Set("user_name", output.Username)
d.Set("type", output.Type)

setTagsOut(ctx, output.Tags)

return diags
}
Loading
Loading