Skip to content

Commit

Permalink
Merge pull request #15830 from DrFaust92/r/sagemaker_notebook_instanc…
Browse files Browse the repository at this point in the history
…e_additional_repos

r/sagemaker_notebook_instance - support additional repos
  • Loading branch information
breathingdust committed Oct 29, 2020
2 parents a982eb0 + 92a7f27 commit ea8e1b5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
23 changes: 23 additions & 0 deletions aws/resource_aws_sagemaker_notebook_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func resourceAwsSagemakerNotebookInstance() *schema.Resource {
Required: true,
ValidateFunc: validation.StringInSlice(sagemaker.InstanceType_Values(), false),
},
"additional_code_repositories": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 3,
Elem: &schema.Schema{Type: schema.TypeString},
},

"volume_size": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -165,6 +171,10 @@ func resourceAwsSagemakerNotebookInstanceCreate(d *schema.ResourceData, meta int
createOpts.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().SagemakerTags()
}

if v, ok := d.GetOk("additional_code_repositories"); ok && v.(*schema.Set).Len() > 0 {
createOpts.AdditionalCodeRepositories = expandStringSet(v.(*schema.Set))
}

log.Printf("[DEBUG] sagemaker notebook instance create config: %#v", *createOpts)
_, err := conn.CreateNotebookInstance(createOpts)
if err != nil {
Expand Down Expand Up @@ -251,6 +261,10 @@ func resourceAwsSagemakerNotebookInstanceRead(d *schema.ResourceData, meta inter
return fmt.Errorf("error setting network_interface_id for sagemaker notebook instance (%s): %w", d.Id(), err)
}

if err := d.Set("additional_code_repositories", flattenStringSet(notebookInstance.AdditionalCodeRepositories)); err != nil {
return fmt.Errorf("error setting additional_code_repositories for sagemaker notebook instance (%s): %s", d.Id(), err)
}

tags, err := keyvaluetags.SagemakerListTags(conn, aws.StringValue(notebookInstance.NotebookInstanceArn))

if err != nil {
Expand Down Expand Up @@ -319,6 +333,15 @@ func resourceAwsSagemakerNotebookInstanceUpdate(d *schema.ResourceData, meta int
hasChanged = true
}

if d.HasChange("additional_code_repositories") {
if v, ok := d.GetOk("additional_code_repositories"); ok {
updateOpts.AdditionalCodeRepositories = expandStringSet(v.(*schema.Set))
} else {
updateOpts.DisassociateAdditionalCodeRepositories = aws.Bool(true)
}
hasChanged = true
}

if hasChanged {

// Stop notebook
Expand Down
74 changes: 74 additions & 0 deletions aws/resource_aws_sagemaker_notebook_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sagemaker/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfawsresource"
)

func init() {
Expand Down Expand Up @@ -94,6 +95,7 @@ func TestAccAWSSagemakerNotebookInstance_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "root_access", "Enabled"),
resource.TestCheckResourceAttr(resourceName, "volume_size", "5"),
resource.TestCheckResourceAttr(resourceName, "default_code_repository", ""),
resource.TestCheckResourceAttr(resourceName, "additional_code_repositories.#", "0"),
resource.TestCheckResourceAttr(resourceName, "security_groups.#", "0"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttrSet(resourceName, "url"),
Expand Down Expand Up @@ -497,6 +499,56 @@ func TestAccAWSSagemakerNotebookInstance_default_code_repository(t *testing.T) {
})
}

func TestAccAWSSagemakerNotebookInstance_additional_code_repositories(t *testing.T) {
var notebook sagemaker.DescribeNotebookInstanceOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
var resourceName = "aws_sagemaker_notebook_instance.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSagemakerNotebookInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSagemakerNotebookInstanceConfigAdditionalCodeRepository1(rName, "https://github.com/terraform-providers/terraform-provider-aws.git"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerNotebookInstanceExists(resourceName, &notebook),
resource.TestCheckResourceAttr(resourceName, "additional_code_repositories.#", "1"),
tfawsresource.TestCheckTypeSetElemAttr(resourceName, "additional_code_repositories.*", "https://github.com/terraform-providers/terraform-provider-aws.git"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSSagemakerNotebookInstanceBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerNotebookInstanceExists(resourceName, &notebook),
resource.TestCheckResourceAttr(resourceName, "additional_code_repositories.#", "0"),
),
},
{
Config: testAccAWSSagemakerNotebookInstanceConfigAdditionalCodeRepository2(rName, "https://github.com/terraform-providers/terraform-provider-aws.git", "https://github.com/hashicorp/terraform.git"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerNotebookInstanceExists(resourceName, &notebook),
resource.TestCheckResourceAttr(resourceName, "additional_code_repositories.#", "2"),
tfawsresource.TestCheckTypeSetElemAttr(resourceName, "additional_code_repositories.*", "https://github.com/terraform-providers/terraform-provider-aws.git"),
tfawsresource.TestCheckTypeSetElemAttr(resourceName, "additional_code_repositories.*", "https://github.com/hashicorp/terraform.git"),
),
},
{
Config: testAccAWSSagemakerNotebookInstanceConfigAdditionalCodeRepository1(rName, "https://github.com/terraform-providers/terraform-provider-aws.git"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerNotebookInstanceExists(resourceName, &notebook),
resource.TestCheckResourceAttr(resourceName, "additional_code_repositories.#", "1"),
tfawsresource.TestCheckTypeSetElemAttr(resourceName, "additional_code_repositories.*", "https://github.com/terraform-providers/terraform-provider-aws.git"),
),
},
},
})
}

func testAccAWSSagemakerNotebookInstanceBaseConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
Expand Down Expand Up @@ -654,6 +706,28 @@ resource "aws_sagemaker_notebook_instance" "test" {
`, rName, defaultCodeRepository)
}

func testAccAWSSagemakerNotebookInstanceConfigAdditionalCodeRepository1(rName, repo1 string) string {
return testAccAWSSagemakerNotebookInstanceBaseConfig(rName) + fmt.Sprintf(`
resource "aws_sagemaker_notebook_instance" "test" {
name = %[1]q
role_arn = aws_iam_role.test.arn
instance_type = "ml.t2.medium"
additional_code_repositories = ["%[2]s"]
}
`, rName, repo1)
}

func testAccAWSSagemakerNotebookInstanceConfigAdditionalCodeRepository2(rName, repo1, repo2 string) string {
return testAccAWSSagemakerNotebookInstanceBaseConfig(rName) + fmt.Sprintf(`
resource "aws_sagemaker_notebook_instance" "test" {
name = %[1]q
role_arn = aws_iam_role.test.arn
instance_type = "ml.t2.medium"
additional_code_repositories = ["%[2]s", "%[3]s"]
}
`, rName, repo1, repo2)
}

func testAccAWSSagemakerNotebookInstanceKMSConfig(rName string) string {
return testAccAWSSagemakerNotebookInstanceBaseConfig(rName) + fmt.Sprintf(`
resource "aws_kms_key" "test" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/sagemaker_notebook_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ The following arguments are supported:
* `root_access` - (Optional) Whether root access is `Enabled` or `Disabled` for users of the notebook instance. The default value is `Enabled`.
* `direct_internet_access` - (Optional) Set to `Disabled` to disable internet access to notebook. Requires `security_groups` and `subnet_id` to be set. Supported values: `Enabled` (Default) or `Disabled`. If set to `Disabled`, the notebook instance will be able to access resources only in your VPC, and will not be able to connect to Amazon SageMaker training and endpoint services unless your configure a NAT Gateway in your VPC.
* `default_code_repository` - (Optional) The Git repository associated with the notebook instance as its default code repository
* `additional_code_repositories` - (Optional) An array of up to three Git repositories to associate with the notebook instance.
These can be either the names of Git repositories stored as resources in your account, or the URL of Git repositories in [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html) or in any other Git repository. These repositories are cloned at the same level as the default repository of your notebook instance.
* `tags` - (Optional) A map of tags to assign to the resource.

## Attributes Reference
Expand Down

0 comments on commit ea8e1b5

Please sign in to comment.