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

F codecatalyst Project #32883

Merged
merged 16 commits into from
Aug 8, 2023
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
4 changes: 4 additions & 0 deletions .changelog/32366.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
```release-note:new-resource
aws_codecatalyst_dev_environment
```

```release-note:note
resource/aws_codecatalyst_dev_environment: Because we cannot easily test this functionality, it is best effort and we ask for community help in testing
```
7 changes: 7 additions & 0 deletions .changelog/32883.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
aws_codecatalyst_project
```

```release-note:note
resource/aws_codecatalyst_project: Because we cannot easily test this functionality, it is best effort and we ask for community help in testing
```
192 changes: 192 additions & 0 deletions internal/service/codecatalyst/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package codecatalyst

import (
"context"
"errors"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codecatalyst"
"github.com/aws/aws-sdk-go-v2/service/codecatalyst/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// Function annotations are used for resource registration to the Provider. DO NOT EDIT.
// @SDKResource("aws_codecatalyst_project", name="Project")
func ResourceProject() *schema.Resource {
return &schema.Resource{

CreateWithoutTimeout: resourceProjectCreate,
ReadWithoutTimeout: resourceProjectRead,
UpdateWithoutTimeout: resourceProjectUpdate,
DeleteWithoutTimeout: resourceProjectDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

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

Schema: map[string]*schema.Schema{
"space_name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"display_name": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

const (
ResNameProject = "Project"
)

func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CodeCatalystClient(ctx)

in := &codecatalyst.CreateProjectInput{
DisplayName: aws.String(d.Get("display_name").(string)),
SpaceName: aws.String(d.Get("space_name").(string)),
Description: aws.String(d.Get("description").(string)),
}

out, err := conn.CreateProject(ctx, in)
if err != nil {
return create.DiagError(names.CodeCatalyst, create.ErrActionCreating, ResNameProject, d.Get("display_name").(string), err)
}

if out == nil || out.Name == nil {
return create.DiagError(names.CodeCatalyst, create.ErrActionCreating, ResNameProject, d.Get("display_name").(string), errors.New("empty output"))
}

d.SetId(aws.ToString(out.Name))
return resourceProjectRead(ctx, d, meta)
}

func resourceProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).CodeCatalystClient(ctx)

spaceName := aws.String(d.Get("space_name").(string))

out, err := findProjectByName(ctx, conn, d.Id(), spaceName)

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

if err != nil {
return append(diags, create.DiagError(names.CodeCatalyst, create.ErrActionReading, ResNameProject, d.Id(), err)...)
}

d.Set("name", out.Name)
d.Set("space_name", out.SpaceName)
d.Set("description", out.Description)

return diags
}

func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).CodeCatalystClient(ctx)

update := false

in := &codecatalyst.UpdateProjectInput{
Name: aws.String(d.Get("display_name").(string)),
SpaceName: aws.String(d.Get("space_name").(string)),
Description: aws.String(d.Get("description").(string)),
}

if d.HasChanges("description") {
in.Description = aws.String(d.Get("description").(string))
update = true
}

if !update {
return diags
}

log.Printf("[DEBUG] Updating Codecatalyst Project (%s): %#v", d.Id(), in)

_, err := conn.UpdateProject(ctx, in)
if err != nil {
return append(diags, create.DiagError(names.CodeCatalyst, create.ErrActionUpdating, ResNameProject, d.Id(), err)...)
}

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

func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CodeCatalystClient(ctx)

log.Printf("[INFO] Deleting CodeCatalyst Project %s", d.Id())

_, err := conn.DeleteProject(ctx, &codecatalyst.DeleteProjectInput{
Name: aws.String(d.Id()),
SpaceName: aws.String(d.Get("space_name").(string)),
})
if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return nil
}

return create.DiagError(names.CodeCatalyst, create.ErrActionDeleting, ResNameProject, d.Id(), err)
}

return nil
}

func findProjectByName(ctx context.Context, conn *codecatalyst.Client, id string, spaceName *string) (*codecatalyst.GetProjectOutput, error) {
in := &codecatalyst.GetProjectInput{
Name: aws.String(id),
SpaceName: spaceName,
}
out, err := conn.GetProject(ctx, in)
if errs.IsA[*types.AccessDeniedException](err) || errs.IsA[*types.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}
if err != nil {
return nil, err
}

if out == nil || out.Name == nil {
return nil, tfresource.NewEmptyResultError(in)
}

return out, nil
}
145 changes: 145 additions & 0 deletions internal/service/codecatalyst/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package codecatalyst_test

import (
"context"
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codecatalyst"
"github.com/aws/aws-sdk-go-v2/service/codecatalyst/types"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
tfcodecatalyst "github.com/hashicorp/terraform-provider-aws/internal/service/codecatalyst"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccCodeCatalystProject_basic(t *testing.T) {
ctx := acctest.Context(t)

var project codecatalyst.GetProjectOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_codecatalyst_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.CodeCatalyst),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckProjectDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccProjectConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckProjectExists(ctx, resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "description", "Sample CC project created by TF"),
resource.TestCheckResourceAttr(resourceName, "space_name", "tf-cc-aws-provider"),
),
},
},
})
}

func TestAccCodeCatalystProject_disappears(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var project codecatalyst.GetProjectOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_codecatalyst_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.CodeCatalyst),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckProjectDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccProjectConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists(ctx, resourceName, &project),
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfcodecatalyst.ResourceProject(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckProjectDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCatalystClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_codecatalyst_project" {
continue
}

spaceName := rs.Primary.Attributes["space_name"]

_, err := conn.GetProject(ctx, &codecatalyst.GetProjectInput{
Name: aws.String(rs.Primary.ID),
SpaceName: aws.String(spaceName),
})
if errs.IsA[*types.AccessDeniedException](err) {
continue
}
if err != nil {
return err
}

return create.Error(names.CodeCatalyst, create.ErrActionCheckingDestroyed, tfcodecatalyst.ResNameProject, rs.Primary.ID, errors.New("not destroyed"))
}

return nil
}
}

func testAccCheckProjectExists(ctx context.Context, name string, project *codecatalyst.GetProjectOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return create.Error(names.CodeCatalyst, create.ErrActionCheckingExistence, tfcodecatalyst.ResNameProject, name, errors.New("not found"))
}

if rs.Primary.ID == "" {
return create.Error(names.CodeCatalyst, create.ErrActionCheckingExistence, tfcodecatalyst.ResNameProject, name, errors.New("not set"))
}

spaceName := rs.Primary.Attributes["space_name"]

conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCatalystClient(ctx)
resp, err := conn.GetProject(ctx, &codecatalyst.GetProjectInput{
Name: aws.String(rs.Primary.ID),
SpaceName: aws.String(spaceName),
})

if err != nil {
return create.Error(names.CodeCatalyst, create.ErrActionCheckingExistence, tfcodecatalyst.ResNameProject, rs.Primary.ID, err)
}

*project = *resp

return nil
}
}

func testAccProjectConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_codecatalyst_project" "test" {
space_name = "tf-cc-aws-provider"
display_name = %[1]q
description = "Sample CC project created by TF"
}
`, rName)
}
5 changes: 5 additions & 0 deletions internal/service/codecatalyst/service_package_gen.go

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

Loading
Loading