Skip to content

Commit

Permalink
add parent argument to listAWSAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegertn committed Jan 17, 2019
1 parent c3f23ab commit 3a329e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ The step returns an array of Account objects with the following fields:
def accounts = listAWSAccounts()
```

You can specify a parent id (Root, Orga unit) with the optional parameter `parent`

```groovy
def accounts = listAWSAccounts('ou-1234-12345678')
```

## updateIdP

Create or update a SAML identity provider with the given metadata document.
Expand Down Expand Up @@ -639,6 +645,7 @@ ec2ShareAmi(
# Changelog

## current master
* add `parent` argument to `listAWSAccounts`

## 1.36
* add `jenkinsStackUpdateStatus` to stack outputs. Specifies if stack was modified
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/de/taimos/pipeline/aws/ListAWSAccountsStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import com.amazonaws.services.organizations.AWSOrganizations;
import com.amazonaws.services.organizations.AWSOrganizationsClientBuilder;
import com.amazonaws.services.organizations.model.Account;
import com.amazonaws.services.organizations.model.ListAccountsForParentRequest;
import com.amazonaws.services.organizations.model.ListAccountsForParentResult;
import com.amazonaws.services.organizations.model.ListAccountsRequest;
import com.amazonaws.services.organizations.model.ListAccountsResult;

Expand All @@ -46,14 +49,21 @@

public class ListAWSAccountsStep extends Step {

private String parent;

@DataBoundConstructor
public ListAWSAccountsStep() {
//
}

@DataBoundSetter
public void setParent(String parent) {
this.parent = parent;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new ListAWSAccountsStep.Execution(context);
return new ListAWSAccountsStep.Execution(this, context);
}

@Extension
Expand All @@ -77,16 +87,19 @@ public String getDisplayName() {

public static class Execution extends SynchronousNonBlockingStepExecution<List> {

public Execution(StepContext context) {
private final transient ListAWSAccountsStep step;

public Execution(ListAWSAccountsStep step, StepContext context) {
super(context);
this.step = step;
}

@Override
protected List run() throws Exception {
this.getContext().get(TaskListener.class).getLogger().format("Getting AWS accounts %n");

AWSOrganizations client = AWSClientFactory.create(AWSOrganizationsClientBuilder.standard(), Execution.this.getContext());
List<Account> accounts = this.getAccounts(client, null);
List<Account> accounts = this.getAccounts(client, this.step.parent, null);

return accounts.stream().map(account -> {
Map<String, String> awsAccount = new HashMap<>();
Expand All @@ -99,11 +112,20 @@ protected List run() throws Exception {
}).collect(Collectors.toList());
}

private List<Account> getAccounts(AWSOrganizations client, String startToken) {
ListAccountsResult result = client.listAccounts(new ListAccountsRequest().withNextToken(startToken));
List<Account> accounts = result.getAccounts();
if (result.getNextToken() != null) {
accounts.addAll(this.getAccounts(client, result.getNextToken()));
private List<Account> getAccounts(AWSOrganizations client, String parent, String startToken) {
final List<Account> accounts;
final String nextToken;
if (parent != null) {
ListAccountsForParentResult result = client.listAccountsForParent(new ListAccountsForParentRequest().withParentId(parent).withNextToken(startToken));
accounts = result.getAccounts();
nextToken = result.getNextToken();
} else {
ListAccountsResult result = client.listAccounts(new ListAccountsRequest().withNextToken(startToken));
accounts = result.getAccounts();
nextToken = result.getNextToken();
}
if (nextToken != null) {
accounts.addAll(this.getAccounts(client, parent, nextToken));
}
return accounts;
}
Expand Down

0 comments on commit 3a329e3

Please sign in to comment.