Skip to content

Commit

Permalink
WIRE-838 - More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwojciski committed Dec 5, 2024
1 parent 175bc80 commit 376516a
Show file tree
Hide file tree
Showing 5 changed files with 596 additions and 343 deletions.
47 changes: 44 additions & 3 deletions castai/resource_organization_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -105,6 +106,13 @@ func resourceOrganizationGroup() *schema.Resource {

func resourceOrganizationGroupCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
organizationID := data.Get(FieldOrganizationGroupOrganizationID).(string)
if organizationID == "" {
var err error
organizationID, err = getDefaultOrganizationId(ctx, meta)
if err != nil {
return diag.FromErr(fmt.Errorf("getting default organization: %w", err))
}
}

client := meta.(*ProviderConfig).api

Expand All @@ -128,6 +136,11 @@ func resourceOrganizationGroupCreate(ctx context.Context, data *schema.ResourceD
}

func resourceOrganizationGroupRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
groupID := data.Id()
if groupID == "" {
return diag.Errorf("group ID is not set")
}

organizationID := data.Get(FieldOrganizationGroupOrganizationID).(string)
if organizationID == "" {
var err error
Expand All @@ -136,7 +149,6 @@ func resourceOrganizationGroupRead(ctx context.Context, data *schema.ResourceDat
return diag.FromErr(fmt.Errorf("getting default organization: %w", err))
}
}
groupID := data.Id()

client := meta.(*ProviderConfig).api

Expand All @@ -153,8 +165,19 @@ func resourceOrganizationGroupRead(ctx context.Context, data *schema.ResourceDat
}

func resourceOrganizationGroupUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
organizationID := data.Get(FieldOrganizationGroupOrganizationID).(string)
groupID := data.Id()
if groupID == "" {
return diag.Errorf("group ID is not set")
}

organizationID := data.Get(FieldOrganizationGroupOrganizationID).(string)
if organizationID == "" {
var err error
organizationID, err = getDefaultOrganizationId(ctx, meta)
if err != nil {
return diag.FromErr(fmt.Errorf("getting default organization: %w", err))
}
}

client := meta.(*ProviderConfig).api

Expand All @@ -175,8 +198,19 @@ func resourceOrganizationGroupUpdate(ctx context.Context, data *schema.ResourceD
}

func resourceOrganizationGroupDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
organizationID := data.Get(FieldOrganizationGroupOrganizationID).(string)
groupID := data.Id()
if groupID == "" {
return diag.Errorf("group ID is not set")
}

organizationID := data.Get(FieldOrganizationGroupOrganizationID).(string)
if organizationID == "" {
var err error
organizationID, err = getDefaultOrganizationId(ctx, meta)
if err != nil {
return diag.FromErr(fmt.Errorf("getting default organization: %w", err))
}
}

client := meta.(*ProviderConfig).api

Expand All @@ -190,6 +224,13 @@ func resourceOrganizationGroupDelete(ctx context.Context, data *schema.ResourceD

func getGroup(client *sdk.ClientWithResponses, ctx context.Context, organizationID, groupID string) (*sdk.CastaiRbacV1beta1Group, error) {
groupsResp, err := client.RbacServiceAPIGetGroupWithResponse(ctx, organizationID, groupID)
if err != nil {
return nil, fmt.Errorf("fetching group: %w", err)
}

if groupsResp.StatusCode() == http.StatusNotFound {
return nil, fmt.Errorf("group %s not found", groupID)
}
if err := sdk.CheckOKResponse(groupsResp, err); err != nil {
return nil, fmt.Errorf("retrieving group: %w", err)
}
Expand Down
Loading

0 comments on commit 376516a

Please sign in to comment.