diff --git a/docs/resources/enterprise_org.md b/docs/resources/enterprise_org.md new file mode 100644 index 00000000..c4cc2fb7 --- /dev/null +++ b/docs/resources/enterprise_org.md @@ -0,0 +1,51 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "astra_enterprise_org Resource - terraform-provider-astra" +subcategory: "" +description: |- + enterprise_org resource represents an Organization that is created under an Enterprise in Astra. +--- + +# astra_enterprise_org (Resource) + +`enterprise_org` resource represents an Organization that is created under an Enterprise in Astra. + +## Example Usage + +```terraform +# Create a new Enterprise organization +resource "astra_enterprise_org" "entorg" { + name = "My Enterprise Organization" + email = "admin@example.com" + admin_user_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d" + enterprise_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d" +} +``` + + +## Schema + +### Required + +- `admin_user_id` (String) UUID of the Astra user that will be the admin of the organization +- `email` (String) Organization email address +- `name` (String) Organization name + +### Read-Only + +- `created_at` (String) The timestamp when the organization was created. +- `enterprise_id` (String) UUID of the Enterprise under which the organization is created +- `id` (String) The ID of this resource. +- `last_modified` (String) The timestamp when the organization was last modified. +- `organization_group_id` (String) The group ID (UUID) of the organization. +- `organization_id` (String) The Astra organization ID (UUID) for the created Enterprise organization. +- `organization_type` (String) The type of the organization. + +## Import + +Import is supported using the following syntax: + +```shell +# The import ID is the organization ID (UUID) +terraform import astra_enterprise_org.example a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d +``` diff --git a/examples/resources/astra_enterprise_org/import.sh b/examples/resources/astra_enterprise_org/import.sh new file mode 100644 index 00000000..8224f1e9 --- /dev/null +++ b/examples/resources/astra_enterprise_org/import.sh @@ -0,0 +1,2 @@ +# The import ID is the organization ID (UUID) +terraform import astra_enterprise_org.example a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d \ No newline at end of file diff --git a/examples/resources/astra_enterprise_org/resource.tf b/examples/resources/astra_enterprise_org/resource.tf new file mode 100644 index 00000000..45fde11e --- /dev/null +++ b/examples/resources/astra_enterprise_org/resource.tf @@ -0,0 +1,7 @@ +# Create a new Enterprise organization +resource "astra_enterprise_org" "entorg" { + name = "My Enterprise Organization" + email = "admin@example.com" + admin_user_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d" + enterprise_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d" +} \ No newline at end of file diff --git a/go.mod b/go.mod index 29c369db..d0943c76 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 toolchain go1.22.0 require ( - github.com/datastax/astra-client-go/v2 v2.2.54 + github.com/datastax/astra-client-go/v2 v2.2.57 github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587 github.com/google/uuid v1.6.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 diff --git a/go.sum b/go.sum index c786d305..1f1cdf48 100644 --- a/go.sum +++ b/go.sum @@ -101,8 +101,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= -github.com/datastax/astra-client-go/v2 v2.2.54 h1:R2k9ek9zaU15cLD96np5gsj12oZhK3Z5/tSytjQagO8= -github.com/datastax/astra-client-go/v2 v2.2.54/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M= +github.com/datastax/astra-client-go/v2 v2.2.57 h1:B2AvCRqWOVBs536r42TpWht1Jt1k2OLsBABLAfN0iVw= +github.com/datastax/astra-client-go/v2 v2.2.57/go.mod h1:zxXWuqDkYia7PzFIL3T7RmjChc9LN81UnfI2yB4kE7M= github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587 h1:3jv+O0hWcz3oj3sZ9/Ov9/m1Vaqx8Ql8jp5ZeA13O5A= github.com/datastax/pulsar-admin-client-go v0.0.0-20230707040954-1a4745e07587/go.mod h1:guL8YZ5gJINN+h5Kmja1AnuzhxLU3sHQL8o/8HYLtqk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 83e5c615..b38ea6b8 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -89,6 +89,7 @@ func NewSDKProvider(version string) func() *schema.Provider { "astra_streaming_sink": resourceStreamingSink(), "astra_table": resourceTable(), "astra_customer_key": resourceCustomerKey(), + "astra_enterprise_org": resourceEnterpriseOrg(), }, Schema: map[string]*schema.Schema{ "token": { diff --git a/internal/provider/resource_enterprise_org.go b/internal/provider/resource_enterprise_org.go new file mode 100644 index 00000000..09c5d184 --- /dev/null +++ b/internal/provider/resource_enterprise_org.go @@ -0,0 +1,140 @@ +package provider + +import ( + "context" + "errors" + + "github.com/datastax/astra-client-go/v2/astra" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceEnterpriseOrg() *schema.Resource { + return &schema.Resource{ + Description: "`enterprise_org` resource represents an Organization that is created under an Enterprise in Astra.", + CreateContext: resourceEnterpriseOrgCreate, + ReadContext: resourceEnterpriseOrgRead, + DeleteContext: resourceEnterpriseOrgDelete, + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: map[string]*schema.Schema{ + // Required + "name": { + Description: "Organization name", + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "email": { + Description: "Organization email address", + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "admin_user_id": { + Description: "UUID of the Astra user that will be the admin of the organization", + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + // Computed + "enterprise_id": { + Description: "UUID of the Enterprise under which the organization is created", + Type: schema.TypeString, + Computed: true, + }, + "organization_id": { + Description: "The Astra organization ID (UUID) for the created Enterprise organization.", + Type: schema.TypeString, + Computed: true, + }, + "organization_type": { + Description: "The type of the organization.", + Type: schema.TypeString, + Computed: true, + }, + "organization_group_id": { + Description: "The group ID (UUID) of the organization.", + Type: schema.TypeString, + Computed: true, + }, + "created_at": { + Description: "The timestamp when the organization was created.", + Type: schema.TypeString, + Computed: true, + }, + "last_modified": { + Description: "The timestamp when the organization was last modified.", + Type: schema.TypeString, + Computed: true, + }, + // TODO: Add MarketPlaceData + }, + } +} + +func resourceEnterpriseOrgCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + client := meta.(astraClients).astraClient.(*astra.ClientWithResponses) + + orgName := d.Get("name").(string) + orgEmail := d.Get("email").(string) + adminUid := d.Get("admin_user_id").(string) + + orgReq := astra.CreateOrganizationInEnterpriseJSONRequestBody{ + Name: orgName, + Email: orgEmail, + AdminUserID: adminUid, + } + + resp, err := client.CreateOrganizationInEnterpriseWithResponse(ctx, orgReq) + if err != nil { + return diag.FromErr(err) + } else if resp.StatusCode() != 201 { + return diag.Errorf("error adding Organization to Enterprise: Status: %s, %s", resp.Status(), resp.Body) + } + + enterpriseOrg := resp.JSON201 + + d.SetId(*enterpriseOrg.OrganizationID) + if err := setEnterpriseOrgData(d, enterpriseOrg); err != nil { + return diag.FromErr(err) + } + return nil +} + +func resourceEnterpriseOrgRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + return diag.Diagnostics{ + diag.Diagnostic{ + Severity: diag.Warning, + Summary: "Read of Enterprise Organizations not supported", + Detail: "Read of Enterprise Organizations not supported.", + }, + } +} + +func resourceEnterpriseOrgDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + return diag.Diagnostics{ + diag.Diagnostic{ + Severity: diag.Warning, + Summary: "Delete of Enterprise Organizations not supported", + Detail: "Delete of Enterprise Organizations not supported.", + }, + } +} + +func setEnterpriseOrgData(d *schema.ResourceData, org *astra.CreateOrgInEnterpriseResponse) error { + if org == nil { + return errors.New("organization is nil") + } + d.Set("enterprise_id", *org.EnterpriseId) + d.Set("organization_id", *org.OrganizationID) + d.Set("organization_type", *org.OrgType) + d.Set("organization_group_id", *org.OrganizationGroupId) + d.Set("created_at", *org.CreatedAt) + d.Set("last_modified", *org.LastModified) + + return nil +}