Skip to content

Commit

Permalink
Add Enterprise Organization operations (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
emerkle826 authored Jun 27, 2024
1 parent 858a644 commit 55b6c5d
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 3 deletions.
51 changes: 51 additions & 0 deletions docs/resources/enterprise_org.md
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
admin_user_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
enterprise_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
}
```

<!-- schema generated by tfplugindocs -->
## 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
```
2 changes: 2 additions & 0 deletions examples/resources/astra_enterprise_org/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The import ID is the organization ID (UUID)
terraform import astra_enterprise_org.example a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d
7 changes: 7 additions & 0 deletions examples/resources/astra_enterprise_org/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Create a new Enterprise organization
resource "astra_enterprise_org" "entorg" {
name = "My Enterprise Organization"
email = "[email protected]"
admin_user_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
enterprise_id = "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
140 changes: 140 additions & 0 deletions internal/provider/resource_enterprise_org.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 55b6c5d

Please sign in to comment.