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

Add Organization Module Sharing Resource #425

Merged
merged 5 commits into from
Feb 2, 2022

Conversation

sebasslash
Copy link
Contributor

Description

Hey Terraformers!

This PR introduces a new resource: tfe_organization_module_sharing which allows site admins to manage how module registries are shared across organizations. This is an admin only resource and thus can only be used in TFE. The resource has 3 total attributes, two of which are arguments and one is a computed attribute:

Arguments:

  • organization - (Required) Name of the organization.
  • module_consumers - (Required) Names of the organizations to consume the module registry.

(Computed) Attributes:

  • consumer_ids - A map of organization names and their opaque, immutable IDs, which look like org-<RANDOM STRING>.

Testing plan

Run acceptance tests:

TESTARGS="-run TestAccTFEOrganizationModuleSharing" make testacc

Smoke test:

  1. As always, you'll need to pull the changes from this PR locally and compile them. In my home folder I've created a dev overrides file ~/tfe_provider-dev.tfrc which I use when testing against the local provider binary:
provider_installation {
    dev_overrides {
        "hashicorp/tfe" = "$GOPATH/bin"
    }

    direct {}
}
  1. Next we'll create a sample configuration. It's important to note an org's module registry DOES NOT NEED existing modules in order to be shared:
resource "tfe_organization" "producer" {
  name = "producer-org"
  email = "[email protected]"
}

resource "tfe_organization" "consumer_1" {
  name = "consumer-org-1"
  email = "[email protected]"
}

resource "tfe_organization" "consumer_2" {
  name = "consumer-org-2"
  email = "[email protected]"
}

resource "tfe_organization_module_sharing" "magic" {
  name = tfe_organization.producer.id
  module_consumers = [tfe_organization.consumer_1.id, tfe_organization.consumer_2.id]
}
  1. Apply and watch the magic happen! Check the terraform.state file for any provider disobedience!
  2. We can "delete" the resource in two ways: set module_consumers to an empty array (this will stop sharing an org's module registry) OR the expected way and delete the resource block in your configuration.

External links

@sebasslash sebasslash requested a review from a team as a code owner January 28, 2022 17:07
Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

Code review only- I haven't tested this yet -- overall very nice

)

func TestAccTFEOrganizationModuleSharing_basic(t *testing.T) {
skipIfFreeOnly(t)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should these be skipped for Terraform Cloud as well?


d.SetId(producer)

return resourceTFEOrganizationModuleSharingUpdate(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.

Something may be amiss- is this entire function body supposed to be this line? Otherwise it looks like we are updating 2x

},

"consumer_ids": {
Type: schema.TypeMap,
Copy link
Collaborator

@brandonc brandonc Jan 31, 2022

Choose a reason for hiding this comment

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

Throughout the provider, org name is synonymous with id (both tfe_organization id/name attributes reflect the name and we don't expose the external id anywhere)

I think it would be less confusing to remove this attribute altogether.

d.SetId("")
return nil
}
return fmt.Errorf("Error reading organization %s mondule consumer list: %w", d.Id(), err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

spelling

Suggested change
return fmt.Errorf("Error reading organization %s mondule consumer list: %w", d.Id(), err)
return fmt.Errorf("Error reading organization %s module consumer list: %w", d.Id(), err)

if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("error deleting organization %s: %w", d.Id(), err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this error looks scary you might want to say that we're having trouble deleting the organization module sharing

This resource represents the ability to manage relationships between organizations
and their registries. It allows you to specify what consumers (organizations) have access
to a given organization's registry. This resource defines two arguments and one computed attribute:

* organization -> the organization to enable module sharing
* module_consumers -> the organizations that will consume the organization's registry
* consumer_ids -> a computed attribute that stores a map of org name => external org id
@sebasslash sebasslash force-pushed the sebasslash/add-module-sharing branch from f7d700e to efc9291 Compare January 31, 2022 22:10
Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

Nice, it seems to work well! We'll have to consider how to mitigate resource drift when we add global_module_sharing to tfe_organization because when you turn it on, it clears the module sharing relationships. When you create module sharing relationships, it turns off global sharing

@sebasslash sebasslash merged commit 89b5f08 into main Feb 2, 2022
@sebasslash sebasslash deleted the sebasslash/add-module-sharing branch February 2, 2022 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants