Skip to content

Commit

Permalink
feat: add is_default property to org
Browse files Browse the repository at this point in the history
feat: add is_default property to org
  • Loading branch information
stebenz authored Oct 24, 2023
2 parents 1887294 + 41799b4 commit c826105
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/org.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ output "org" {

### Read-Only

- `is_default` (Boolean) Indicates whether the org is the default org of the instance.
- `name` (String) Name of the org.
- `primary_domain` (String) Primary domain of the org
- `state` (String) State of the org, supported values: ORG_STATE_UNSPECIFIED, ORG_STATE_ACTIVE, ORG_STATE_INACTIVE, ORG_STATE_REMOVED
4 changes: 4 additions & 0 deletions docs/resources/org.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ resource "zitadel_org" "default" {

- `name` (String) Name of the org

### Optional

- `is_default` (Boolean) True sets the org as default org for the instance. Only one org can be default org. Nothing happens if you set it to false until you set another org as default org.

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
1 change: 1 addition & 0 deletions zitadel/org/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
OrgIDVar = "id"
orgIDsVar = "ids"
NameVar = "name"
IsDefaultVar = "is_default"
nameMethodVar = "name_method"
DomainVar = "domain"
domainMethodVar = "domain_method"
Expand Down
5 changes: 5 additions & 0 deletions zitadel/org/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func GetDatasource() *schema.Resource {
Computed: true,
Description: "Name of the org.",
},
IsDefaultVar: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the org is the default org of the instance.",
},
stateVar: {
Type: schema.TypeString,
Computed: true,
Expand Down
62 changes: 52 additions & 10 deletions zitadel/org/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ func create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia
if err != nil {
return diag.FromErr(err)
}
d.SetId(resp.GetId())
orgId := resp.GetId()
d.SetId(orgId)
if val, ok := d.GetOk(IsDefaultVar); ok && val.(bool) {
adminClient, err := helper.GetAdminClient(clientinfo)
if err != nil {
return diag.FromErr(err)
}
_, err = adminClient.SetDefaultOrg(ctx, &admin.SetDefaultOrgRequest{
OrgId: orgId,
})
if err != nil {
return diag.Errorf("error while setting default org id %s: %v", orgId, err)
}
}
return nil
}

Expand All @@ -60,16 +73,32 @@ func update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia
if !ok {
return diag.Errorf("failed to get client")
}
client, err := helper.GetManagementClient(clientinfo)
if err != nil {
return diag.FromErr(err)
}
// If try updating the name to the same value API will return an error.
if d.HasChange(NameVar) {
client, err := helper.GetManagementClient(clientinfo)
if err != nil {
return diag.FromErr(err)
}

_, err = client.UpdateOrg(helper.CtxSetOrgID(ctx, d.Id()), &management.UpdateOrgRequest{
Name: d.Get(NameVar).(string),
})
if err != nil {
return diag.Errorf("failed to update org: %v", err)
_, err = client.UpdateOrg(helper.CtxSetOrgID(ctx, d.Id()), &management.UpdateOrgRequest{
Name: d.Get(NameVar).(string),
})
if err != nil {
return diag.Errorf("failed to update org: %v", err)
}
}
// To unset the default org, we need to set another org as default org.
if isDefault, ok := d.GetOk(IsDefaultVar); ok && isDefault.(bool) && d.HasChange(IsDefaultVar) {
adminClient, err := helper.GetAdminClient(clientinfo)
if err != nil {
return diag.FromErr(err)
}
_, err = adminClient.SetDefaultOrg(ctx, &admin.SetDefaultOrgRequest{
OrgId: d.Id(),
})
if err != nil {
return diag.Errorf("error while setting default org id %s: %v", d.Id(), err)
}
}
return nil
}
Expand Down Expand Up @@ -103,6 +132,19 @@ func get(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagno
if err := d.Set(stateVar, state); err != nil {
return diag.Errorf("error while setting org state %s: %v", state, err)
}
adminClient, err := helper.GetAdminClient(clientinfo)
if err != nil {
return diag.FromErr(err)
}
defaultOrg, err := adminClient.GetDefaultOrg(ctx, &admin.GetDefaultOrgRequest{})
if err != nil {
return diag.Errorf("error while getting default instance org: %v", err)
}
if defaultOrg.Org.Id == remoteOrg.Id {
if err := d.Set(IsDefaultVar, true); err != nil {
return diag.Errorf("error while setting org is_default: %v", err)
}
}
return nil
}

Expand Down
11 changes: 11 additions & 0 deletions zitadel/org/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ func GetResource() *schema.Resource {
Required: true,
Description: "Name of the org",
},
IsDefaultVar: {
Type: schema.TypeBool,
Optional: true,
DiffSuppressOnRefresh: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// Need to avoid forever pending changes when trying to set this to false
// since setting to false will not actually unmark the org as default.
return new != "true"
},
Description: "True sets the org as default org for the instance. Only one org can be default org. Nothing happens if you set it to false until you set another org as default org.",
},
primaryDomainVar: {
Type: schema.TypeString,
Computed: true,
Expand Down

0 comments on commit c826105

Please sign in to comment.