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

Service Catalog Provisioned Product and related resources #13797

Closed
Show file tree
Hide file tree
Changes from 66 commits
Commits
Show all changes
168 commits
Select commit Hold shift + click to select a range
f6321f3
refactoring artifact definition
bw-intuit Oct 25, 2017
8915edc
wip adding artifact block
bw-intuit Oct 26, 2017
f365893
refactoring to support new format
bw-intuit Nov 13, 2017
bfa2bc2
minor code cleanup
bw-intuit Nov 13, 2017
9227706
update based on code review
bw-intuit Apr 9, 2018
9370c2d
updates based on code review
bw-intuit Apr 10, 2018
0e57346
refactoring to updated template structure
bw-intuit May 3, 2018
07107de
fix test
bw-intuit May 3, 2018
ea45424
minor code / test cleanup
bw-intuit May 3, 2018
051a1a0
adding comment
bw-intuit May 3, 2018
5179004
reading template url, fix import
bw-intuit May 3, 2018
88f81f0
refactored, added acceptance tests and documentation
trung Jun 21, 2018
22148da
minor fixes
trung Jun 25, 2018
8870e42
initial testing
May 23, 2020
e887f5f
hope and prayer
May 23, 2020
b261bbd
found the work of @trung and getting it up and running
May 23, 2020
c01782c
intial creation working
May 25, 2020
bea44a7
updates
May 26, 2020
383dbfd
Merge remote-tracking branch 'trung/f-service-catalog' into sc-produc…
ahgittin Jun 12, 2020
975fd2d
Merge remote-tracking branch 'frc9/master' into sc-product-and-pp
ahgittin Jun 12, 2020
848689c
Merge branch 'master' into sc-product-and-pp
ahgittin Jun 12, 2020
413cd8a
update servicecatalog_product helper imports
kemitix Jun 11, 2020
dee8315
revert go.mod and go.sum
ahgittin Jun 12, 2020
56493a6
fix error handling on PP
ahgittin Jun 12, 2020
1d8c7a6
fix syntax errors in servicecatalog product tests
ahgittin Jun 12, 2020
bfaadb1
include TODO items for things not supported on product
ahgittin Jun 12, 2020
243dc9a
provisioned product - create, read, and delete minimally working
ahgittin Jun 12, 2020
440ef4b
Merge branch 'master' into sc-product-and-pp
kemitix Jun 15, 2020
676b9d0
sc-product: define timeouts for update and delete operations
kemitix Jun 15, 2020
eb1a397
sc-product: generate idempotencyToken as resource id
kemitix Jun 15, 2020
258271d
sc-product: extract waitFor…Status method
kemitix Jun 15, 2020
93fbd22
sc-provisioned-product: add wait for resource to create
kemitix Jun 15, 2020
24f04a2
Merge remote-tracking branch 'origin/master' into sc-product-and-pp
ahgittin Jun 15, 2020
1818eac
minor corrections to product doc, and add a useful "complete" example
ahgittin Jun 15, 2020
3b98f38
Merge branch 'sc-product-and-pp' of github.com:cloudsoft/terraform-pr…
ahgittin Jun 15, 2020
63763ef
sc-product-portfolio-assoc: crud
kemitix Jun 15, 2020
7a15c8c
add docs for service catalog provisioned product and portfolio-produc…
ahgittin Jun 16, 2020
222fea6
Merge branch 'sc-product-and-pp' of github.com:cloudsoft/terraform-pr…
ahgittin Jun 16, 2020
971ac5b
sc-product-portfolio-assoc: cast ids to string early
kemitix Jun 16, 2020
edc5efd
sc-product-portfolio-assoc: fetch all pages ListPortfoliosForProduct
kemitix Jun 16, 2020
b24619a
sc-product-portfolio-assoc: rename methods to fit existing pattern
kemitix Jun 16, 2020
25ab24a
update PP and add'l fields
ahgittin Jun 16, 2020
07c104a
Merge branch 'sc-product-and-pp' of github.com:cloudsoft/terraform-pr…
ahgittin Jun 16, 2020
610384d
SC PP - support params, tags; better wait logic
ahgittin Jun 16, 2020
4e025ed
SC - Product - wait for completion of tasks, not just submission
ahgittin Jun 16, 2020
3c661c6
sc-product-portfolio-assoc: register in provider.go
kemitix Jun 16, 2020
1b8d9f3
sc-product-portfolio-assoc: first pass at basic test
kemitix Jun 16, 2020
e1327f5
wait for, and test, deletion of SC Product and SC ProvisionedProduct
ahgittin Jun 16, 2020
515b1cd
Merge branch 'sc-product-and-pp' of github.com:cloudsoft/terraform-pr…
ahgittin Jun 16, 2020
0b7e87e
sc-product-portfolio-assoc: minor updates to docs
kemitix Jun 16, 2020
7377257
fix ID syntax for portfolio-product-association -- all tests passing,…
ahgittin Jun 16, 2020
c94fab4
sc-product-portfolio-assoc: revert to standard format for arguments
kemitix Jun 16, 2020
035dfac
Merge branch 'sc-product-and-pp' of github.com:cloudsoft/terraform-pr…
ahgittin Jun 16, 2020
59fa151
sc-provisioned-product: fix typo in docs
kemitix Jun 16, 2020
c34bb97
add portfolio-principal association, and use it for product provision…
ahgittin Jun 16, 2020
d22fcd8
Merge branch 'sc-product-and-pp' of github.com:cloudsoft/terraform-pr…
ahgittin Jun 17, 2020
888aa0b
sc-portfolio-assocs: Add todo notes to reduce api calls
kemitix Jun 17, 2020
06af8fb
sc-portfolio-assocs: only fetch pages when assoc not yet found
kemitix Jun 17, 2020
9294fb1
fix the product provisioning test, ensure use of separate providers a…
ahgittin Jun 17, 2020
80ff8af
Merge branch 'sc-product-and-pp' of github.com:cloudsoft/terraform-pr…
ahgittin Jun 17, 2020
f096f5d
Merge branch 'master' into sc-product-and-pp
ahgittin Jun 17, 2020
f5ca63e
SC provisioned-product test passing!
ahgittin Jun 17, 2020
ae3e74d
tidy up - merge util class with the (only) place it is used
ahgittin Jun 17, 2020
bc84989
apply gofmt
ahgittin Jun 17, 2020
9793677
SC - more miny tidies and explanations
ahgittin Jun 17, 2020
b8b8b4b
shorten prefixes to fit within 20 char limit
ahgittin Jun 17, 2020
866f047
fix most linter-flagged issues
ahgittin Jun 17, 2020
19fca67
address go lint issues
ahgittin Jun 17, 2020
603d288
address linter issues in docs page (deprecated frontmatter)
ahgittin Jun 17, 2020
eee00a1
SC - address first round of PR review comments
ahgittin Jun 18, 2020
08716d3
revert the index files - not to have any SC items (to avoid merge con…
ahgittin Jun 18, 2020
70ac64a
remove provisioned product additions for now, focus on incremental PRs
ahgittin Jun 18, 2020
625c9b7
remove portfolio-principal-association additions for now, focus on in…
ahgittin Jun 18, 2020
2dcb083
remove portfolio-product-association additions for now, focus on incr…
ahgittin Jun 18, 2020
85d33e4
remove product additions and portfolio changes for now, focus on incr…
ahgittin Jun 18, 2020
9af48f5
restore product additions and portfolio changes, focus on incremental…
ahgittin Jun 18, 2020
db3e1aa
improve product tests to conform with conventions
ahgittin Jun 18, 2020
8cfecab
restore portfolio-principal-association additions for now, focus on i…
ahgittin Jun 18, 2020
038fd4a
restore portfolio-product-association additions for now, focus on inc…
ahgittin Jun 18, 2020
0551515
add product to index files
ahgittin Jun 18, 2020
46303a4
add portfolio-principal association to index files
ahgittin Jun 18, 2020
6983acb
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Jun 18, 2020
c655600
add portfolio-product association to index files
ahgittin Jun 18, 2020
3994bf4
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Jun 18, 2020
dfbc50f
Revert "remove provisioned product additions for now, focus on increm…
ahgittin Jun 18, 2020
9cea7db
add provisioned product to index files
ahgittin Jun 18, 2020
bdce9ea
add checks on AWS output - name and whether product has been re-creat…
ahgittin Jun 18, 2020
a57bf3f
test improvements - add disappears test, simplify template
ahgittin Jun 19, 2020
256bd32
correct ID format in docs
ahgittin Jun 19, 2020
a7f6049
add and update all tests, all passing
ahgittin Jun 19, 2020
08a888d
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Jun 19, 2020
ccba736
Fix two linter problems
ahgittin Jun 19, 2020
822ac3f
remove "update" (all replaces), backfill additional tests, all passing
ahgittin Jun 19, 2020
c5870a1
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Jun 22, 2020
763f582
Merge branch 'f-servicecatalog-portfolio-product-association' into f-…
ahgittin Jun 22, 2020
4724b3e
update product_test method names to match prevailing conventions
ahgittin Jun 22, 2020
11d3c81
update portfolio_principal_association_test method names to match pre…
ahgittin Jun 22, 2020
7dddfab
update portfolio_product_association_test method names to match preva…
ahgittin Jun 22, 2020
9ac47f6
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Jun 22, 2020
9a2da04
Merge branch 'f-servicecatalog-product' into f-servicecatalog-provisi…
ahgittin Jun 22, 2020
7c4981c
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Jun 22, 2020
cd321eb
Merge branch 'f-servicecatalog-portfolio-product-association' into f-…
ahgittin Jun 22, 2020
98c2c44
SC product - fix bug in tag removal
ahgittin Jun 22, 2020
242ee56
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Jun 22, 2020
ecb78d4
Merge branch 'f-servicecatalog-portfolio-product-association' into f-…
ahgittin Jun 22, 2020
90a0a2c
update portfolio_test method names to match prevailing conventions
ahgittin Jun 22, 2020
c22f20e
Merge branch 'td-servicecatalog-portfolio' into f-servicecatalog-prov…
ahgittin Jun 22, 2020
ee6d2cf
update SC provisioned_product_test
ahgittin Jun 22, 2020
9ba34d8
remove changes to Portfolio here; do them in another PR
ahgittin Jun 22, 2020
4320102
rearrange imports to match convention
ahgittin Jun 22, 2020
5717d01
add note on using the SC portfolio to the docs
ahgittin Jun 22, 2020
4cee871
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Jun 22, 2020
0c70f23
Merge branch 'f-servicecatalog-product' into f-servicecatalog-provisi…
ahgittin Jun 22, 2020
26e4e36
Merge branch 'f-servicecatalog-portfolio-product-association' into f-…
ahgittin Jun 22, 2020
0158cd0
Merge branch 'td-servicecatalog-portfolio' into f-servicecatalog-prov…
ahgittin Jun 22, 2020
c8c96cd
add new fields, get tests working
ahgittin Jun 22, 2020
a2da3ee
SC provisioned product - change params and outputs to map, add test f…
ahgittin Jun 22, 2020
0eb761d
replace deprecated `"${x}"` expressions with just `x`
ahgittin Jun 25, 2020
d37ad72
Merge branch 'f-servicecatalog-product' into f-servicecatalog-provisi…
ahgittin Jun 25, 2020
05c6044
SC product - use standard method for SC tags from input
ahgittin Jul 10, 2020
39a5a7a
SC product - remove pointless extra conversion of map from SC tags
ahgittin Jul 10, 2020
2ed98dc
SC product - code review - refactor extracting methods and add valida…
ahgittin Jul 21, 2020
24cb977
SC Product - change tests to be very very much like the recommended f…
ahgittin Jul 21, 2020
81ba7eb
Merge branch 'f-servicecatalog-product' into f-servicecatalog-provisi…
ahgittin Jul 21, 2020
131d9ed
Merge branch 'master' into f-servicecatalog-product
ahgittin Jul 21, 2020
a0da08f
conform markdown examples to new terrafmt
ahgittin Jul 21, 2020
312d002
Merge branch 'f-servicecatalog-product' into f-servicecatalog-provisi…
ahgittin Jul 21, 2020
862fb04
SC Product - remove region on bucket, it no longer works
ahgittin Jul 21, 2020
40a91d1
SC Portfolio Principal Association - tests updated
ahgittin Jul 21, 2020
87e6ab0
SC Portfolio Principal Association - conform markdown examples to ter…
ahgittin Jul 21, 2020
fd79364
Merge branch 'master' into f-servicecatalog-portfolio-principal-assoc…
ahgittin Jul 21, 2020
f5076f3
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Jul 21, 2020
aa3a333
SC Portfolio Product Association - conform markdown examples to terra…
ahgittin Jul 21, 2020
3214eac
SC Portfolio Product Association - tests updated
ahgittin Jul 21, 2020
a8576b5
Merge branch 'master' into td-servicecatalog-portfolio
ahgittin Jul 21, 2020
d41a8c6
SC Portfolio - update tests, esp disappears test
ahgittin Jul 21, 2020
978af98
Merge branch 'f-servicecatalog-portfolio-product-association' into f-…
ahgittin Jul 21, 2020
b03c4c4
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Jul 21, 2020
2f7c275
Merge branch 'td-servicecatalog-portfolio' into f-servicecatalog-prov…
ahgittin Jul 21, 2020
c69e597
SC PP - conform markdown examples to terrafmt requirements
ahgittin Jul 21, 2020
2192e12
SC Provisioned Product - update tests
ahgittin Jul 21, 2020
f1b3c20
SC Portfolio Principal Association - test for principal existence
ahgittin Jul 21, 2020
eecd2fd
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Jul 21, 2020
be3065b
SC Portfolio Principal Association - format tidy
ahgittin Jul 21, 2020
e1d1d3b
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Jul 21, 2020
1294114
SC Portfolio Principal Association - fix principal check
ahgittin Jul 21, 2020
1e57005
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Jul 21, 2020
da120b2
Merge branch 'master' into f-servicecatalog-product
ahgittin Sep 26, 2020
2d6833b
Update imports to use v2/ of plugin SDK
ahgittin Sep 26, 2020
ec3aaed
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Sep 26, 2020
597a929
Update imports to use v2/ of plugin SDK
ahgittin Sep 26, 2020
33be215
Merge branch 'master' into f-servicecatalog-portfolio-principal-assoc…
ahgittin Sep 26, 2020
c8bae8b
Update imports to use v2/ of plugin SDK
ahgittin Sep 26, 2020
8c4f3bb
Merge branch 'master' into td-servicecatalog-portfolio
ahgittin Sep 26, 2020
a6ec0ca
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Sep 26, 2020
2703aa7
Merge branch 'f-servicecatalog-portfolio-product-association' into f-…
ahgittin Sep 26, 2020
d90c3f1
Merge branch 'td-servicecatalog-portfolio' into f-servicecatalog-prov…
ahgittin Sep 26, 2020
ef4b2d1
Update imports to use v2/ of plugin SDK
ahgittin Sep 26, 2020
9c9dd75
fix trailing spaces in markdown
ahgittin Sep 26, 2020
08e3b91
fix trailing spaces in markdown
ahgittin Sep 26, 2020
e65df4f
fix markdown indentation
ahgittin Sep 26, 2020
7dbf42f
Merge branch 'f-servicecatalog-product' into f-servicecatalog-portfol…
ahgittin Sep 26, 2020
5eef5df
fix trailing spaces in markdown
ahgittin Sep 26, 2020
9e7e621
Merge branch 'f-servicecatalog-portfolio-product-association' into f-…
ahgittin Sep 26, 2020
0818862
Merge branch 'f-servicecatalog-portfolio-principal-association' into …
ahgittin Sep 26, 2020
fc440d5
fix markdown trailing space
ahgittin Sep 26, 2020
a61501d
fix markdown trailing spaces
ahgittin Sep 26, 2020
9da757f
Merge branch 'td-servicecatalog-portfolio' into f-servicecatalog-prov…
ahgittin Sep 26, 2020
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 aws/provider.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,10 @@ func Provider() terraform.ResourceProvider {
"aws_securityhub_product_subscription": resourceAwsSecurityHubProductSubscription(),
"aws_securityhub_standards_subscription": resourceAwsSecurityHubStandardsSubscription(),
"aws_servicecatalog_portfolio": resourceAwsServiceCatalogPortfolio(),
"aws_servicecatalog_portfolio_principal_association": resourceAwsServiceCatalogPortfolioPrincipalAssociation(),
"aws_servicecatalog_portfolio_product_association": resourceAwsServiceCatalogPortfolioProductAssociation(),
"aws_servicecatalog_product": resourceAwsServiceCatalogProduct(),
"aws_servicecatalog_provisioned_product": resourceAwsServiceCatalogProvisionedProduct(),
"aws_service_discovery_http_namespace": resourceAwsServiceDiscoveryHttpNamespace(),
"aws_service_discovery_private_dns_namespace": resourceAwsServiceDiscoveryPrivateDnsNamespace(),
"aws_service_discovery_public_dns_namespace": resourceAwsServiceDiscoveryPublicDnsNamespace(),
Expand Down
165 changes: 165 additions & 0 deletions aws/resource_aws_servicecatalog_portfolio_principal_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package aws

import (
"fmt"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit, can you split the deps to go packages and external dep packages like other resources?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done - all files (check-in to come)

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"strings"
"time"
)

func resourceAwsServiceCatalogPortfolioPrincipalAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsServiceCatalogPortfolioPrincipalAssociationCreate,
Read: resourceAwsServiceCatalogPortfolioPrincipalAssociationRead,
Update: resourceAwsServiceCatalogPortfolioPrincipalAssociationUpdate,
Delete: resourceAwsServiceCatalogPortfolioPrincipalAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"portfolio_id": {
Type: schema.TypeString,
Required: true,
},
"principal_arn": {
Type: schema.TypeString,
Required: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add validation here using ValidateFunc: validateArn,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (check-in to come)

},
},
}
}

func resourceAwsServiceCatalogPortfolioPrincipalAssociationCreate(d *schema.ResourceData, meta interface{}) error {
_, portfolioId, principalArn := resourceAwsServiceCatalogPortfolioPrincipalAssociationRequiredParameters(d)
input := servicecatalog.AssociatePrincipalWithPortfolioInput{
PortfolioId: aws.String(portfolioId),
PrincipalARN: aws.String(principalArn),
PrincipalType: aws.String("IAM"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change hardcoded "IAM" to servicecatalog.PrincipalTypeIam

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (check-in to come)

}
conn := meta.(*AWSClient).scconn
_, err := conn.AssociatePrincipalWithPortfolio(&input)
if err != nil {
return fmt.Errorf("creating Service Catalog Principal(%s)/Portfolio(%s) Association failed: %s",
principalArn, portfolioId, err.Error())
}

stateConf := resource.StateChangeConf{
Pending: []string{servicecatalog.StatusCreating},
Target: []string{servicecatalog.StatusAvailable},
Timeout: 1 * time.Minute,
PollInterval: 3 * time.Second,
Refresh: func() (interface{}, string, error) {
err := resourceAwsServiceCatalogPortfolioPrincipalAssociationRead(d, meta)
if err != nil {
return 42, "", err
}
if d.Id() != "" {
return 42, servicecatalog.StatusAvailable, err
}
return 0, servicecatalog.StatusCreating, err
},
}
_, err = stateConf.WaitForState()
return err
}

func resourceAwsServiceCatalogPortfolioPrincipalAssociationRead(d *schema.ResourceData, meta interface{}) error {
id, portfolioId, principalArn := resourceAwsServiceCatalogPortfolioPrincipalAssociationRequiredParameters(d)
input := servicecatalog.ListPrincipalsForPortfolioInput{
PortfolioId: aws.String(portfolioId),
}
conn := meta.(*AWSClient).scconn
var pageToken = ""
isFound := false
for {
pageOfDetails, nextPageToken, err := resourceAwsServiceCatalogPortfolioPrincipalAssociationListPrincipalsForPortfolioPage(conn, input, &pageToken)
if err != nil {
return err
}
for _, principal := range pageOfDetails {
if *principal.PrincipalARN == principalArn {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap principal.PrincipalARN with aws.StringValue()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (check-in to come)

isFound = true
d.SetId(id)
break
}
}
if nextPageToken == nil || isFound {
break
}
pageToken = *nextPageToken
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (check-in to come)

}
if !isFound {
log.Printf("[WARN] Service Catalog Principal(%s)/Portfolio(%s) Association not found, removing from state",
principalArn, portfolioId)
d.SetId("")
}
d.Set("principal_arn", principalArn)
d.Set("portfolio_id", portfolioId)
return nil
}

func resourceAwsServiceCatalogPortfolioPrincipalAssociationListPrincipalsForPortfolioPage(conn *servicecatalog.ServiceCatalog, input servicecatalog.ListPrincipalsForPortfolioInput, nextPageToken *string) ([]*servicecatalog.Principal, *string, error) {
input.PageToken = nextPageToken
var page, err = conn.ListPrincipalsForPortfolio(&input)
if err != nil {
return nil, nil, fmt.Errorf("retrieving Service Catalog Associations for Principal/Portfolios: %s", err.Error())
}
principalDetails := page.Principals
return principalDetails, page.NextPageToken, nil
}

func resourceAwsServiceCatalogPortfolioPrincipalAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
const principalArnKey = "principal_arn"
const portfolioIdKey = "portfolio_id"
if d.HasChange(principalArnKey) || d.HasChange(portfolioIdKey) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to a single d.HasChanges(principalArnKey, portfolioIdKey)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, any reason for the consts? using the string literals are the way its done in other resources

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the linter picked these up and already changed (already checked in)

the only reason for the constants was that in some projects that's preferred (to prevent typos) but we've switched to this project's convention of repreated string literals

oldPrincipalArn, newPrincipalArn := d.GetChange(principalArnKey)
oldPortfolioId, newPortfolioId := d.GetChange(portfolioIdKey)
d.Set(principalArnKey, oldPrincipalArn)
d.Set(portfolioIdKey, oldPortfolioId)
resourceAwsServiceCatalogPortfolioPrincipalAssociationDelete(d, meta)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit unusual as far as i can tell, same thing for the call to create. can we extract the logic to other functions and re use them? update should only call read

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure about this one:

The only way to "update" this resource is to delete the old and create a new. So this implementation seemed a good way. But happy to change it. Options I see are:

(a) As is -- call the canonical "delete" and "create" methods
(b) Refactor the common delete (and create) logic eg to a doDelete (and doCreate) which gets used by the canonical "delete" ("create") and "update"
(c) Just duplicate the delete and create logic here
(d) Mark the fields as ForceNew: true and make this method return an UnexpectedInvocation error

What do you recommend?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(d) seems the right way: as soon as all fields are ForceNew it complains if the update method even exists (nice)

d.Set(principalArnKey, newPrincipalArn)
d.Set(portfolioIdKey, newPortfolioId)
resourceAwsServiceCatalogPortfolioPrincipalAssociationCreate(d, meta)
}
return resourceAwsServiceCatalogPortfolioPrincipalAssociationRead(d, meta)
}

func resourceAwsServiceCatalogPortfolioPrincipalAssociationDelete(d *schema.ResourceData, meta interface{}) error {
_, portfolioId, principalArn := resourceAwsServiceCatalogPortfolioPrincipalAssociationRequiredParameters(d)
input := servicecatalog.DisassociatePrincipalFromPortfolioInput{
PortfolioId: aws.String(portfolioId),
PrincipalARN: aws.String(principalArn),
}
conn := meta.(*AWSClient).scconn
_, err := conn.DisassociatePrincipalFromPortfolio(&input)
if err != nil {
return fmt.Errorf("deleting Service Catalog Principal(%s)/Portfolio(%s) Association failed: %s",
principalArn, portfolioId, err.Error())
}
return nil
}

func resourceAwsServiceCatalogPortfolioPrincipalAssociationRequiredParameters(d *schema.ResourceData) (string, string, string) {
if principalArn, ok := d.GetOk("principal_arn"); ok {
portfolioId := d.Get("portfolio_id").(string)
id := portfolioId + "--" + principalArn.(string)
return id, portfolioId, principalArn.(string)
}
return parseServiceCatalogPortfolioPrincipalAssociationResourceId(d.Id())
}

func parseServiceCatalogPortfolioPrincipalAssociationResourceId(id string) (string, string, string) {
s := strings.SplitN(id, "--", 2)
portfolioId := s[0]
principalArn := s[1]
return id, portfolioId, principalArn
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"testing"
)

func TestAccAWSServiceCatalogPortfolioPrincipalAssociation_Basic(t *testing.T) {
salt := acctest.RandString(5)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckServiceCatalogPortfolioPrincipalAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigBasic(salt),
Check: testAccCheckAwsServiceCatalogPortfolioPrincipalAssociation(),
},
{
ResourceName: "aws_servicecatalog_portfolio_principal_association.association",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAwsServiceCatalogPortfolioPrincipalAssociation() resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).scconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_servicecatalog_portfolio_principal_association" {
continue // not our monkey
}
_, portfolioId, principalArn := parseServiceCatalogPortfolioPrincipalAssociationResourceId(rs.Primary.ID)
input := servicecatalog.ListPrincipalsForPortfolioInput{PortfolioId: &portfolioId}
page, err := conn.ListPrincipalsForPortfolio(&input)
if err != nil {
return err
}
for _, principalDetail := range page.Principals {
if *principalDetail.PrincipalARN == principalArn {
return nil //is good
}
}
return fmt.Errorf("association not found between portfolio %s and principal %s", portfolioId, principalArn)
}
return fmt.Errorf("no associations found")
}
}

func testAccCheckServiceCatalogPortfolioPrincipalAssociationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).scconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_servicecatalog_portfolio_principal_association" {
continue // not our monkey
}
_, portfolioId, principalArn := parseServiceCatalogPortfolioPrincipalAssociationResourceId(rs.Primary.ID)
input := servicecatalog.ListPrincipalsForPortfolioInput{PortfolioId: &portfolioId}
page, err := conn.ListPrincipalsForPortfolio(&input)
if err != nil {
if isAWSErr(err, servicecatalog.ErrCodeResourceNotFoundException, "") {
return nil // not found for principal is good
}
return err // some other unexpected error
}
for _, principalDetail := range page.Principals {
if *principalDetail.PrincipalARN == principalArn {
return fmt.Errorf("expected AWS Service Catalog Portfolio Principal Association to be gone, but it was still found")
}
}
}
return nil
}

func testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigBasic(salt string) string {
return testAccCheckAwsServiceCatalogPortfolioResourceConfigBasic("tfm-test-"+salt) + "\n" +
testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigRoleAndAssociation(salt)
}

func testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigRole(salt string) string {
roleName := "tfm-sc-tester-" + salt
return fmt.Sprintf(`
# IAM
resource "aws_iam_role" "tfm-sc-tester" {
name = "%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": { "AWS": "*" },
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
`, roleName)
}

func testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigAssociation() string {
return fmt.Sprintf(`
resource "aws_servicecatalog_portfolio_principal_association" "association" {
portfolio_id = aws_servicecatalog_portfolio.test.id
principal_arn = aws_iam_role.tfm-sc-tester.arn
}`)
}

func testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigRoleAndAssociation(salt string) string {
return testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigRole(salt) +
testAccCheckAwsServiceCatalogPortfolioPrincipalAssociationConfigAssociation()
}
Loading