-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for OCI Registry feed (#801)
- Loading branch information
1 parent
d832a3b
commit 4b7caef
Showing
11 changed files
with
365 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "octopusdeploy_oci_registry_feed Resource - terraform-provider-octopusdeploy" | ||
subcategory: "" | ||
description: |- | ||
This resource manages a OCI Registry feed in Octopus Deploy. | ||
--- | ||
|
||
# octopusdeploy_oci_registry_feed (Resource) | ||
|
||
This resource manages a OCI Registry feed in Octopus Deploy. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "octopusdeploy_oci_registry_feed" "example" { | ||
feed_uri = "oci://test-registry.docker.io" | ||
password = "test-password" | ||
name = "Test oci Registry Feed (OK to Delete)" | ||
username = "test-username" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `feed_uri` (String) | ||
- `name` (String) The name of this resource. | ||
|
||
### Optional | ||
|
||
- `password` (String, Sensitive) The password associated with this resource. | ||
- `space_id` (String) The space ID associated with this OCI registry. | ||
- `username` (String, Sensitive) The username associated with this resource. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The unique ID for this resource. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
terraform import [options] octopusdeploy_oci_registry_feed.<name> <feed-id> | ||
``` |
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 @@ | ||
terraform import [options] octopusdeploy_oci_registry_feed.<name> <feed-id> |
6 changes: 6 additions & 0 deletions
6
examples/resources/octopusdeploy_oci_registry_feed/resource.tf
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,6 @@ | ||
resource "octopusdeploy_oci_registry_feed" "example" { | ||
feed_uri = "oci://test-registry.docker.io" | ||
password = "test-password" | ||
name = "Test oci Registry Feed (OK to Delete)" | ||
username = "test-username" | ||
} |
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
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,168 @@ | ||
package octopusdeploy_framework | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
|
||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
type ociRegistryFeedTypeResource struct { | ||
*Config | ||
} | ||
|
||
func NewOCIRegistryFeedResource() resource.Resource { | ||
return &ociRegistryFeedTypeResource{} | ||
} | ||
|
||
var _ resource.ResourceWithImportState = &ociRegistryFeedTypeResource{} | ||
|
||
func (r *ociRegistryFeedTypeResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = util.GetTypeName("oci_registry_feed") | ||
} | ||
|
||
func (r *ociRegistryFeedTypeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schemas.OCIRegistryFeedSchema{}.GetResourceSchema() | ||
} | ||
|
||
func (r *ociRegistryFeedTypeResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
r.Config = ResourceConfiguration(req, resp) | ||
} | ||
|
||
func (r *ociRegistryFeedTypeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var data *schemas.OCIRegistryFeedTypeResourceModel | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
feed, err := createOCIRegistryResourceFromData(data) | ||
if err != nil { | ||
return | ||
} | ||
|
||
tflog.Info(ctx, fmt.Sprintf("creating OCI Registry feed: %s", feed.GetName())) | ||
|
||
client := r.Config.Client | ||
createdFeed, err := feeds.Add(client, feed) | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to create OCI Registry feed", err.Error()) | ||
return | ||
} | ||
|
||
updateDataFromOCIRegistryFeed(data, data.SpaceID.ValueString(), createdFeed.(*feeds.OCIRegistryFeed)) | ||
|
||
tflog.Info(ctx, fmt.Sprintf("OCI Registry feed created (%s)", data.ID)) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r *ociRegistryFeedTypeResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data *schemas.OCIRegistryFeedTypeResourceModel | ||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
tflog.Info(ctx, fmt.Sprintf("reading OCI Registry feed (%s)", data.ID)) | ||
|
||
client := r.Config.Client | ||
feed, err := feeds.GetByID(client, data.SpaceID.ValueString(), data.ID.ValueString()) | ||
if err != nil { | ||
if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "OCI Registry feed"); err != nil { | ||
resp.Diagnostics.AddError("unable to load OCI Registry feed", err.Error()) | ||
} | ||
return | ||
} | ||
|
||
loadedFeed := feed.(*feeds.OCIRegistryFeed) | ||
updateDataFromOCIRegistryFeed(data, data.SpaceID.ValueString(), loadedFeed) | ||
|
||
tflog.Info(ctx, fmt.Sprintf("OCI Registry feed read (%s)", loadedFeed.GetID())) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r *ociRegistryFeedTypeResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var data, state *schemas.OCIRegistryFeedTypeResourceModel | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("updating OCI Registry feed '%s'", data.ID.ValueString())) | ||
|
||
feed, err := createOCIRegistryResourceFromData(data) | ||
feed.ID = state.ID.ValueString() | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to load OCI Registry feed", err.Error()) | ||
return | ||
} | ||
|
||
tflog.Info(ctx, fmt.Sprintf("updating OCI Registry feed (%s)", data.ID)) | ||
|
||
client := r.Config.Client | ||
updatedFeed, err := feeds.Update(client, feed) | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to update OCI Registry feed", err.Error()) | ||
return | ||
} | ||
|
||
updateDataFromOCIRegistryFeed(data, state.SpaceID.ValueString(), updatedFeed.(*feeds.OCIRegistryFeed)) | ||
|
||
tflog.Info(ctx, fmt.Sprintf("OCI Registry feed updated (%s)", data.ID)) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r *ociRegistryFeedTypeResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var data schemas.OCIRegistryFeedTypeResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if err := feeds.DeleteByID(r.Config.Client, data.SpaceID.ValueString(), data.ID.ValueString()); err != nil { | ||
resp.Diagnostics.AddError("unable to delete OCI Registry feed", err.Error()) | ||
return | ||
} | ||
} | ||
|
||
func createOCIRegistryResourceFromData(data *schemas.OCIRegistryFeedTypeResourceModel) (*feeds.OCIRegistryFeed, error) { | ||
feed, err := feeds.NewOCIRegistryFeed(data.Name.ValueString()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
feed.ID = data.ID.ValueString() | ||
feed.FeedURI = data.FeedUri.ValueString() | ||
|
||
feed.Username = data.Username.ValueString() | ||
feed.Password = core.NewSensitiveValue(data.Password.ValueString()) | ||
feed.SpaceID = data.SpaceID.ValueString() | ||
|
||
return feed, nil | ||
} | ||
|
||
func updateDataFromOCIRegistryFeed(data *schemas.OCIRegistryFeedTypeResourceModel, spaceId string, feed *feeds.OCIRegistryFeed) { | ||
data.FeedUri = types.StringValue(feed.FeedURI) | ||
data.Name = types.StringValue(feed.Name) | ||
data.SpaceID = types.StringValue(spaceId) | ||
if feed.Username != "" { | ||
data.Username = types.StringValue(feed.Username) | ||
} | ||
|
||
data.ID = types.StringValue(feed.ID) | ||
} | ||
|
||
func (*ociRegistryFeedTypeResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} |
91 changes: 91 additions & 0 deletions
91
octopusdeploy_framework/resource_oci_registry_feed_test.go
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,91 @@ | ||
package octopusdeploy_framework | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
"testing" | ||
) | ||
|
||
type ociRegistryFeedTestData struct { | ||
name string | ||
uri string | ||
username string | ||
password string | ||
} | ||
|
||
func TestAccOctopusDeployOCIRegistryFeed(t *testing.T) { | ||
localName := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
prefix := "octopusdeploy_oci_registry_feed." + localName | ||
createData := ociRegistryFeedTestData{ | ||
name: acctest.RandStringFromCharSet(20, acctest.CharSetAlpha), | ||
uri: "oci://integration-test-registry.docker.io", | ||
username: acctest.RandStringFromCharSet(20, acctest.CharSetAlpha), | ||
password: acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum), | ||
} | ||
updateData := ociRegistryFeedTestData{ | ||
name: createData.name + "-updated", | ||
uri: "oci://integration-test-registry-updated.docker.io", | ||
username: createData.username + "-changed", | ||
password: createData.password + "-generated", | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
CheckDestroy: func(s *terraform.State) error { return testOCIRegistryFeedCheckDestroy(s) }, | ||
PreCheck: func() { TestAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: ProtoV6ProviderFactories(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testOCIRegistryFeedBasic(createData, localName), | ||
Check: testAssertOCIRegistryAttributes(createData, prefix), | ||
}, | ||
{ | ||
Config: testOCIRegistryFeedBasic(updateData, localName), | ||
Check: testAssertOCIRegistryAttributes(updateData, prefix), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAssertOCIRegistryAttributes(expected ociRegistryFeedTestData, prefix string) resource.TestCheckFunc { | ||
return resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(prefix, "name", expected.name), | ||
resource.TestCheckResourceAttr(prefix, "feed_uri", expected.uri), | ||
resource.TestCheckResourceAttr(prefix, "username", expected.username), | ||
resource.TestCheckResourceAttr(prefix, "password", expected.password), | ||
) | ||
} | ||
|
||
func testOCIRegistryFeedBasic(data ociRegistryFeedTestData, localName string) string { | ||
return fmt.Sprintf(` | ||
resource "octopusdeploy_oci_registry_feed" "%s" { | ||
name = "%s" | ||
feed_uri = "%s" | ||
username = "%s" | ||
password = "%s" | ||
} | ||
`, | ||
localName, | ||
data.name, | ||
data.uri, | ||
data.username, | ||
data.password, | ||
) | ||
} | ||
|
||
func testOCIRegistryFeedCheckDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "octopusdeploy_oci_registry_feed" { | ||
continue | ||
} | ||
|
||
feed, err := feeds.GetByID(octoClient, octoClient.GetSpaceID(), rs.Primary.ID) | ||
if err == nil && feed != nil { | ||
return fmt.Errorf("OCI Registry feed (%s) still exists", rs.Primary.ID) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.