-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
247eec9
commit a604183
Showing
4 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/docker/terraform-provider-docker/internal/pkg/hubclient" | ||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var ( | ||
_ resource.Resource = &OrgMemberAssociationResource{} | ||
_ resource.ResourceWithConfigure = &OrgMemberAssociationResource{} | ||
_ resource.ResourceWithImportState = &OrgMemberAssociationResource{} | ||
) | ||
|
||
func NewOrgMemberAssociationResource() resource.Resource { | ||
return &OrgMemberAssociationResource{} | ||
} | ||
|
||
type OrgMemberAssociationResource struct { | ||
client *hubclient.Client | ||
} | ||
|
||
type OrgMemberAssociationResourceModel struct { | ||
OrgName types.String `tfsdk:"org_name"` | ||
TeamName types.String `tfsdk:"team_name"` | ||
UserName types.String `tfsdk:"user_name"` | ||
Role types.String `tfsdk:"role"` // New field for role | ||
InviteID types.String `tfsdk:"invite_id"` // This is needed for deletion | ||
} | ||
|
||
func (r *OrgMemberAssociationResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
|
||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*hubclient.Client) | ||
if !ok { | ||
errMsg := fmt.Sprintf("Expected *hubclient.Client, got: %T", req.ProviderData) | ||
resp.Diagnostics.AddError("Unexpected Resource Configure Type", errMsg) | ||
return | ||
} | ||
|
||
r.client = client | ||
} | ||
|
||
func (r *OrgMemberAssociationResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_org_member_association" | ||
} | ||
|
||
func (r *OrgMemberAssociationResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "Manages the association of a member with a team in an organization.", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"org_name": schema.StringAttribute{ | ||
MarkdownDescription: "Organization name", | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
}, | ||
"team_name": schema.StringAttribute{ | ||
MarkdownDescription: "Team name within the organization", | ||
Required: false, | ||
Optional: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
}, | ||
"user_name": schema.StringAttribute{ | ||
MarkdownDescription: "User name (email) of the member being associated with the team", | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
}, | ||
"role": schema.StringAttribute{ | ||
MarkdownDescription: "Role assigned to the user within the organization (e.g., 'member', 'admin').", | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
Validators: []validator.String{ | ||
stringvalidator.OneOf("member", "editor", "owner"), | ||
}, | ||
}, | ||
"invite_id": schema.StringAttribute{ | ||
MarkdownDescription: "The ID of the invite. Used for managing the association, especially for deletion.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *OrgMemberAssociationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
|
||
var data OrgMemberAssociationResourceModel | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
inviteResp, err := r.client.InviteOrgMember(ctx, data.OrgName.ValueString(), data.TeamName.ValueString(), data.Role.ValueString(), []string{data.UserName.ValueString()}, false) | ||
if err != nil { | ||
errMsg := fmt.Sprintf("Unable to create org_member_association resource: %v", err) | ||
resp.Diagnostics.AddError("Error Creating Resource", errMsg) | ||
return | ||
} | ||
|
||
if len(inviteResp.OrgInvitees) == 0 { | ||
errMsg := "Invite failed: No invitees were returned from the Docker Hub API." | ||
resp.Diagnostics.AddError("Invite Failed", errMsg) | ||
return | ||
} | ||
|
||
invite := inviteResp.OrgInvitees[0] | ||
data.InviteID = types.StringValue(invite.Invite.ID) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
// TODO: finish read | ||
func (r *OrgMemberAssociationResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
|
||
var data OrgMemberAssociationResourceModel | ||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
// TODO: setup update | ||
func (r *OrgMemberAssociationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
return | ||
} | ||
|
||
func (r *OrgMemberAssociationResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var data OrgMemberAssociationResourceModel | ||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Deleting an established member (accepted inv) vs deleting an invited member has different API calls | ||
// Invited members that have not accepted do not have a recorded username in the org afaik | ||
// Attempt to delete by inviteID first | ||
err := r.client.DeleteOrgInvite(ctx, data.InviteID.ValueString()) | ||
if err != nil { | ||
// If deleting by inviteID fails, try deleting by orgName and userName | ||
err = r.client.DeleteOrgMember(ctx, data.OrgName.ValueString(), data.UserName.ValueString()) | ||
if err != nil { | ||
errMsg := fmt.Sprintf("Unable to delete org_member_association resource: %v", err) | ||
log.Println(errMsg) | ||
resp.Diagnostics.AddError("Error Deleting Resource", errMsg) | ||
return | ||
} | ||
} | ||
|
||
resp.State.RemoveResource(ctx) | ||
log.Println("Successfully deleted OrgMemberAssociationResource.") | ||
} | ||
|
||
// TODO: setup import state | ||
func (r *OrgMemberAssociationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccOrgMemberAssociationResource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testOrgMemberAssociationResourceConfig(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("docker_org_member_association.test", "invite_id"), | ||
resource.TestCheckResourceAttr("docker_org_member_association.test", "org_name", "dockerhackathon"), | ||
resource.TestCheckResourceAttr("docker_org_member_association.test", "team_name", "test"), | ||
resource.TestCheckResourceAttr("docker_org_member_association.test", "user_name", "[email protected]"), | ||
resource.TestCheckResourceAttr("docker_org_member_association.test", "role", "member"), | ||
), | ||
}, | ||
// { | ||
// ResourceName: "docker_org_member_association.test", | ||
// ImportState: false, | ||
// ImportStateVerify: false, | ||
// ImportStateVerifyIgnore: []string{"invite_id"}, | ||
// }, | ||
}, | ||
}) | ||
} | ||
|
||
func testOrgMemberAssociationResourceConfig() string { | ||
return ` | ||
provider "docker" { | ||
} | ||
resource "docker_org_member_association" "test" { | ||
org_name = "dockerhackathon" | ||
team_name = "test" | ||
user_name = "[email protected]" | ||
role = "member" | ||
} | ||
` | ||
} |