-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32899 from nrajb/f-CodeCatalyst-SourceRepo
f-CodeCatalyst-SourceRepository
- Loading branch information
Showing
5 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:resource | ||
aws_codecatalyst_source_repository | ||
``` | ||
|
||
```release-note:note | ||
resource/aws_codecatalyst_source_repository: Because we cannot easily test this functionality, it is best effort and we ask for community help in testing | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// 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_source_repository", name="Source Repository") | ||
func ResourceSourceRepository() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateWithoutTimeout: resourceSourceRepositoryCreate, | ||
ReadWithoutTimeout: resourceSourceRepositoryRead, | ||
UpdateWithoutTimeout: resourceSourceRepositoryCreate, | ||
DeleteWithoutTimeout: resourceSourceRepositoryDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Update: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"project_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"space_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
const ( | ||
ResNameSourceRepository = "Source Repository" | ||
) | ||
|
||
func resourceSourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
|
||
conn := meta.(*conns.AWSClient).CodeCatalystClient(ctx) | ||
|
||
in := &codecatalyst.CreateSourceRepositoryInput{ | ||
Name: aws.String(d.Get("name").(string)), | ||
ProjectName: aws.String(d.Get("project_name").(string)), | ||
SpaceName: aws.String(d.Get("space_name").(string)), | ||
} | ||
|
||
out, err := conn.CreateSourceRepository(ctx, in) | ||
if err != nil { | ||
return append(diags, create.DiagError(names.CodeCatalyst, create.ErrActionCreating, ResNameSourceRepository, d.Get("name").(string), err)...) | ||
} | ||
|
||
if out == nil || out.Name == nil { | ||
return append(diags, create.DiagError(names.CodeCatalyst, create.ErrActionCreating, ResNameSourceRepository, d.Get("name").(string), errors.New("empty output"))...) | ||
} | ||
|
||
d.SetId(aws.ToString(out.Name)) | ||
|
||
return resourceSourceRepositoryRead(ctx, d, meta) | ||
} | ||
|
||
func resourceSourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).CodeCatalystClient(ctx) | ||
|
||
projectName := aws.String(d.Get("project_name").(string)) | ||
spaceName := aws.String(d.Get("space_name").(string)) | ||
|
||
out, err := findSourceRepositoryByName(ctx, conn, d.Id(), projectName, spaceName) | ||
|
||
if !d.IsNewResource() && tfresource.NotFound(err) { | ||
log.Printf("[WARN] CodeCatalyst SourceRepository (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return diags | ||
} | ||
|
||
if err != nil { | ||
return append(diags, create.DiagError(names.CodeCatalyst, create.ErrActionReading, ResNameSourceRepository, d.Id(), err)...) | ||
} | ||
|
||
d.Set("name", out.Name) | ||
d.Set("project_name", out.ProjectName) | ||
d.Set("space_name", out.SpaceName) | ||
d.Set("description", out.Description) | ||
|
||
return diags | ||
} | ||
|
||
func resourceSourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
|
||
conn := meta.(*conns.AWSClient).CodeCatalystClient(ctx) | ||
|
||
log.Printf("[INFO] Deleting CodeCatalyst SourceRepository %s", d.Id()) | ||
|
||
_, err := conn.DeleteSourceRepository(ctx, &codecatalyst.DeleteSourceRepositoryInput{ | ||
Name: aws.String(d.Id()), | ||
ProjectName: aws.String(d.Get("project_name").(string)), | ||
SpaceName: aws.String(d.Get("space_name").(string)), | ||
}) | ||
|
||
if errs.IsA[*types.ResourceNotFoundException](err) { | ||
return diags | ||
} | ||
if err != nil { | ||
return append(diags, create.DiagError(names.CodeCatalyst, create.ErrActionDeleting, ResNameSourceRepository, d.Id(), err)...) | ||
} | ||
|
||
return diags | ||
} | ||
|
||
func findSourceRepositoryByName(ctx context.Context, conn *codecatalyst.Client, name string, projectName, spaceName *string) (*codecatalyst.GetSourceRepositoryOutput, error) { | ||
in := &codecatalyst.GetSourceRepositoryInput{ | ||
Name: aws.String(name), | ||
ProjectName: projectName, | ||
SpaceName: spaceName, | ||
} | ||
out, err := conn.GetSourceRepository(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 | ||
} |
153 changes: 153 additions & 0 deletions
153
internal/service/codecatalyst/source_repository_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// 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-testing/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 TestAccCodeCatalystSourceRepository_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
var sourcerepository codecatalyst.GetSourceRepositoryOutput | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
resourceName := "aws_codecatalyst_source_repository.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acctest.PreCheck(ctx, t) | ||
acctest.PreCheckPartitionHasService(t, names.CodeCatalyst) | ||
testAccPreCheck(ctx, t) | ||
}, | ||
ErrorCheck: acctest.ErrorCheck(t, names.CodeCatalyst), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckSourceRepositoryDestroy(ctx), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSourceRepositoryConfig_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSourceRepositoryExists(ctx, resourceName, &sourcerepository), | ||
resource.TestCheckResourceAttr(resourceName, "name", rName), | ||
resource.TestCheckResourceAttr(resourceName, "space_name", "tf-cc-aws-provider"), | ||
resource.TestCheckResourceAttr(resourceName, "project_name", "tf-cc"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccCodeCatalystSourceRepository_disappears(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
var sourcerepository codecatalyst.GetSourceRepositoryOutput | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
resourceName := "aws_codecatalyst_source_repository.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acctest.PreCheck(ctx, t) | ||
acctest.PreCheckPartitionHasService(t, names.CodeCatalyst) | ||
testAccPreCheck(ctx, t) | ||
}, | ||
ErrorCheck: acctest.ErrorCheck(t, names.CodeCatalyst), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckSourceRepositoryDestroy(ctx), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSourceRepositoryConfig_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSourceRepositoryExists(ctx, resourceName, &sourcerepository), | ||
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfcodecatalyst.ResourceSourceRepository(), resourceName), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckSourceRepositoryDestroy(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_source_repository" { | ||
continue | ||
} | ||
spaceName := rs.Primary.Attributes["space_name"] | ||
projectName := rs.Primary.Attributes["project_name"] | ||
|
||
input := &codecatalyst.GetSourceRepositoryInput{ | ||
Name: aws.String(rs.Primary.ID), | ||
SpaceName: aws.String(spaceName), | ||
ProjectName: aws.String(projectName), | ||
} | ||
_, err := conn.GetSourceRepository(ctx, input) | ||
|
||
if errs.IsA[*types.AccessDeniedException](err) { | ||
continue | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return create.Error(names.CodeCatalyst, create.ErrActionCheckingDestroyed, tfcodecatalyst.ResNameSourceRepository, rs.Primary.ID, errors.New("not destroyed")) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckSourceRepositoryExists(ctx context.Context, name string, sourcerepository *codecatalyst.GetSourceRepositoryOutput) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return create.Error(names.CodeCatalyst, create.ErrActionCheckingExistence, tfcodecatalyst.ResNameSourceRepository, name, errors.New("not found")) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return create.Error(names.CodeCatalyst, create.ErrActionCheckingExistence, tfcodecatalyst.ResNameSourceRepository, name, errors.New("not set")) | ||
} | ||
spaceName := rs.Primary.Attributes["space_name"] | ||
projectName := rs.Primary.Attributes["project_name"] | ||
|
||
conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCatalystClient(ctx) | ||
resp, err := conn.GetSourceRepository(ctx, &codecatalyst.GetSourceRepositoryInput{ | ||
Name: aws.String(rs.Primary.ID), | ||
SpaceName: aws.String(spaceName), | ||
ProjectName: aws.String(projectName), | ||
}) | ||
|
||
if err != nil { | ||
return create.Error(names.CodeCatalyst, create.ErrActionCheckingExistence, tfcodecatalyst.ResNameSourceRepository, rs.Primary.ID, err) | ||
} | ||
|
||
*sourcerepository = *resp | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccSourceRepositoryConfig_basic(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_codecatalyst_source_repository" "test" { | ||
name = %[1]q | ||
project_name = "tf-cc" | ||
space_name = "tf-cc-aws-provider" | ||
} | ||
`, rName) | ||
} |
Oops, something went wrong.