-
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.
- Loading branch information
1 parent
fd21a66
commit 7dacbf9
Showing
10 changed files
with
313 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
1 change: 1 addition & 0 deletions
1
examples/resources/octopusdeploy_artifactory_generic_feed/import.sh
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_artifactory_generic_feed.<name> <feed-id> |
8 changes: 8 additions & 0 deletions
8
examples/resources/octopusdeploy_artifactory_generic_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,8 @@ | ||
resource "octopusdeploy_artifactory_generic_feed" "example" { | ||
feed_uri = "https://example.jfrog.io/" | ||
password = "test-password" | ||
name = "Test Artifactory Generic Feed (OK to Delete)" | ||
username = "test-username" | ||
repository = "repo" | ||
layout_regex = "this is regex" | ||
} |
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,108 @@ | ||
package octopusdeploy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceArtifactoryGenericFeed() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceArtifactoryGenericFeedCreate, | ||
DeleteContext: resourceArtifactoryGenericFeedDelete, | ||
Description: "This resource manages a Artifactory Generic feed in Octopus Deploy.", | ||
Importer: getImporter(), | ||
ReadContext: resourceArtifactoryGenericFeedRead, | ||
Schema: getArtifactoryGenericFeedSchema(), | ||
UpdateContext: resourceArtifactoryGenericFeedUpdate, | ||
} | ||
} | ||
|
||
func resourceArtifactoryGenericFeedCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
artifactoryGenericFeed, err := expandArtifactoryGenericFeed(d) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
tflog.Info(ctx, fmt.Sprintf("creating Artifactory Generic feed: %s", artifactoryGenericFeed.GetName())) | ||
|
||
client := m.(*client.Client) | ||
createdFeed, err := feeds.Add(client, artifactoryGenericFeed) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := setArtifactoryGenericFeed(ctx, d, createdFeed.(*feeds.ArtifactoryGenericFeed)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(createdFeed.GetID()) | ||
|
||
tflog.Info(ctx, fmt.Sprintf("Artifactory Generic feed created (%s)", d.Id())) | ||
return nil | ||
} | ||
|
||
func resourceArtifactoryGenericFeedDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
tflog.Info(ctx, fmt.Sprintf("deleting Artifactory Generic feed (%s)", d.Id())) | ||
|
||
client := m.(*client.Client) | ||
err := feeds.DeleteByID(client, d.Get("space_id").(string), d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId("") | ||
|
||
tflog.Info(ctx, "Artifactory Generic feed deleted") | ||
return nil | ||
} | ||
|
||
func resourceArtifactoryGenericFeedRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
tflog.Info(ctx, fmt.Sprintf("reading Artifactory Generic feed (%s)", d.Id())) | ||
|
||
client := m.(*client.Client) | ||
feed, err := feeds.GetByID(client, d.Get("space_id").(string), d.Id()) | ||
|
||
if err != nil { | ||
return errors.ProcessApiError(ctx, d, err, "Artifactory Generic feed") | ||
} | ||
|
||
if err := setArtifactoryGenericFeed(ctx, d, feed.(*feeds.ArtifactoryGenericFeed)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
tflog.Info(ctx, fmt.Sprintf("Artifactory Generic feed read (%s)", feed.GetID())) | ||
return nil | ||
} | ||
|
||
func resourceArtifactoryGenericFeedUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
feed, err := expandArtifactoryGenericFeed(d) | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("layout_regex from schema: %s", d.Get("layout_regex").(string))) | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("layout regex from feed model: %s", feed.LayoutRegex)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
tflog.Info(ctx, fmt.Sprintf("updating Artifactory Generic feed (%s)", feed.GetID())) | ||
|
||
client := m.(*client.Client) | ||
updatedFeed, err := feeds.Update(client, feed) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := setArtifactoryGenericFeed(ctx, d, updatedFeed.(*feeds.ArtifactoryGenericFeed)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
tflog.Info(ctx, fmt.Sprintf("Artifactory Generic feed updated (%s)", d.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package octopusdeploy | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccOctopusDeployArtifactoryGenericFeed(t *testing.T) { | ||
localName := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
prefix := "octopusdeploy_artifactory_generic_feed." + localName | ||
|
||
feedURI := "https://example.jfrog.io" | ||
name := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
password := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
username := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
repository := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
layoutRegex := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
CheckDestroy: testArtifactoryGenericFeedCheckDestroy, | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Check: resource.ComposeTestCheckFunc( | ||
testArtifactoryGenericFeedExists(prefix), | ||
resource.TestCheckResourceAttr(prefix, "feed_uri", feedURI), | ||
resource.TestCheckResourceAttr(prefix, "name", name), | ||
resource.TestCheckResourceAttr(prefix, "password", password), | ||
resource.TestCheckResourceAttr(prefix, "username", username), | ||
resource.TestCheckResourceAttr(prefix, "repository", repository), | ||
resource.TestCheckResourceAttr(prefix, "layout_regex", layoutRegex), | ||
), | ||
Config: testArtifactoryGenericFeedBasic(localName, feedURI, name, username, password, repository, layoutRegex), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testArtifactoryGenericFeedBasic(localName string, feedURI string, name string, username string, password string, repository string, layoutRegex string) string { | ||
return fmt.Sprintf(`resource "octopusdeploy_artifactory_generic_feed" "%s" { | ||
feed_uri = "%s" | ||
name = "%s" | ||
password = "%s" | ||
username = "%s" | ||
repository = "%s" | ||
layout_regex = "%s" | ||
}`, localName, feedURI, name, password, username, repository, layoutRegex) | ||
} | ||
|
||
func testArtifactoryGenericFeedExists(prefix string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*client.Client) | ||
feedID := s.RootModule().Resources[prefix].Primary.ID | ||
if _, err := client.Feeds.GetByID(feedID); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testArtifactoryGenericFeedCheckDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "octopusdeploy_artifactory_generic_feed" { | ||
continue | ||
} | ||
|
||
client := testAccProvider.Meta().(*client.Client) | ||
feed, err := client.Feeds.GetByID(rs.Primary.ID) | ||
if err == nil && feed != nil { | ||
return fmt.Errorf("Artifactory Generic 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package octopusdeploy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/feeds" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func expandArtifactoryGenericFeed(d *schema.ResourceData) (*feeds.ArtifactoryGenericFeed, error) { | ||
name := d.Get("name").(string) | ||
|
||
feed, err := feeds.NewArtifactoryGenericFeed(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
feed.ID = d.Id() | ||
|
||
if v, ok := d.GetOk("feed_uri"); ok { | ||
feed.FeedURI = v.(string) | ||
} | ||
|
||
if v, ok := d.GetOk("package_acquisition_location_options"); ok { | ||
feed.PackageAcquisitionLocationOptions = getSliceFromTerraformTypeList(v) | ||
} | ||
|
||
if v, ok := d.GetOk("password"); ok { | ||
feed.Password = core.NewSensitiveValue(v.(string)) | ||
} | ||
|
||
if v, ok := d.GetOk("space_id"); ok { | ||
feed.SpaceID = v.(string) | ||
} | ||
|
||
if v, ok := d.GetOk("username"); ok { | ||
feed.Username = v.(string) | ||
} | ||
|
||
if v, ok := d.GetOk("layout_regex"); ok { | ||
feed.LayoutRegex = v.(string) | ||
} | ||
|
||
if v, ok := d.GetOk("repository"); ok { | ||
feed.Repository = v.(string) | ||
} | ||
|
||
return feed, nil | ||
} | ||
|
||
func getArtifactoryGenericFeedSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"feed_uri": { | ||
Required: true, | ||
Type: schema.TypeString, | ||
}, | ||
"id": getIDSchema(), | ||
"name": { | ||
Description: "A short, memorable, unique name for this feed. Example: ACME Builds.", | ||
Required: true, | ||
Type: schema.TypeString, | ||
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty), | ||
}, | ||
"package_acquisition_location_options": { | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Optional: true, | ||
Type: schema.TypeList, | ||
}, | ||
"password": getPasswordSchema(false), | ||
"space_id": getSpaceIDSchema(), | ||
"username": getUsernameSchema(false), | ||
"repository": { | ||
Computed: false, | ||
Required: true, | ||
Type: schema.TypeString, | ||
}, | ||
"layout_regex": { | ||
Computed: false, | ||
Required: false, | ||
Optional: true, | ||
Type: schema.TypeString, | ||
}, | ||
} | ||
} | ||
|
||
func setArtifactoryGenericFeed(ctx context.Context, d *schema.ResourceData, feed *feeds.ArtifactoryGenericFeed) error { | ||
d.Set("feed_uri", feed.FeedURI) | ||
d.Set("name", feed.Name) | ||
d.Set("space_id", feed.SpaceID) | ||
d.Set("username", feed.Username) | ||
d.Set("repository", feed.Repository) | ||
d.Set("layout_regex", feed.LayoutRegex) | ||
|
||
if err := d.Set("package_acquisition_location_options", feed.PackageAcquisitionLocationOptions); err != nil { | ||
return fmt.Errorf("error setting package_acquisition_location_options: %s", err) | ||
} | ||
|
||
d.SetId(feed.GetID()) | ||
|
||
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