Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use bundle instead of sdk-go-container-registry #752

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ require (
github.com/iancoleman/strcase v0.3.0
github.com/ionos-cloud/sdk-go-api-gateway v1.0.0
github.com/ionos-cloud/sdk-go-bundle/products/cdn/v2 v2.1.0
github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2 v2.0.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to remove the old entry
github.com/ionos-cloud/sdk-go-container-registry v1.2.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

github.com/ionos-cloud/sdk-go-bundle/products/logging/v2 v2.1.1
github.com/ionos-cloud/sdk-go-bundle/products/monitoring/v2 v2.0.0
github.com/ionos-cloud/sdk-go-bundle/products/vpn/v2 v2.0.2
github.com/ionos-cloud/sdk-go-bundle/shared v0.1.1
github.com/ionos-cloud/sdk-go-cert-manager v1.3.0
github.com/ionos-cloud/sdk-go-container-registry v1.2.0
github.com/ionos-cloud/sdk-go-dataplatform v1.1.1
github.com/ionos-cloud/sdk-go-dbaas-in-memory-db v1.0.1
github.com/ionos-cloud/sdk-go-dbaas-mariadb v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ github.com/ionos-cloud/sdk-go-api-gateway v1.0.0 h1:T+JzarV1Iw7NjWQfEuv5bESnHoCL
github.com/ionos-cloud/sdk-go-api-gateway v1.0.0/go.mod h1:TkSlamP+qKRT8cgBaZEN8rawanVluOR0dXbXXdmmwTY=
github.com/ionos-cloud/sdk-go-bundle/products/cdn/v2 v2.1.0 h1:8fRICa4i+GJHrCgnLe8t3uwfahkg7pcDDwoLkH3B2Xc=
github.com/ionos-cloud/sdk-go-bundle/products/cdn/v2 v2.1.0/go.mod h1:BF6R+j1GdJEMImxkeh32qjIVWqsBfgi6VgyfsY5ezIA=
github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2 v2.0.2 h1:+f8+Gfnm/a1ZkBpw643sUDG7Zin8Paj7PPHFHLc9XbY=
github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2 v2.0.2/go.mod h1:MqxTZ40QJv2yq2qlNAzIABRwCKV4oDf7tRSbtwQIk0g=
github.com/ionos-cloud/sdk-go-bundle/products/logging/v2 v2.1.1 h1:GXxdNKl7haQnHO1QewZeXazur5+LDCpHqvP+nNObGaA=
github.com/ionos-cloud/sdk-go-bundle/products/logging/v2 v2.1.1/go.mod h1:N0ifRq3ENqGg6qht1WPrVgSkcavPS5SRWlNfkHUZbfQ=
github.com/ionos-cloud/sdk-go-bundle/products/monitoring/v2 v2.0.0 h1:gVh+jd0o8ROtJLWqUB6/VU8vSLdj/kRYAz/qbMO7z7I=
Expand All @@ -126,8 +128,6 @@ github.com/ionos-cloud/sdk-go-bundle/shared v0.1.1 h1:NWobgIhmUJKG6ohFXefMj+KmpE
github.com/ionos-cloud/sdk-go-bundle/shared v0.1.1/go.mod h1:cLIl4zmB1yMdPSNgTzaRmXeuusEAY1O3Lp/iXSc/R6E=
github.com/ionos-cloud/sdk-go-cert-manager v1.3.0 h1:VMbD/XgLmMV2d7uI1+xf/uzRZWKJd8Ayan9IZ6gH1pM=
github.com/ionos-cloud/sdk-go-cert-manager v1.3.0/go.mod h1:8CPWJBuryfrUpiPNrVIPry6qQjZWfIhmRpFkqKiaO2w=
github.com/ionos-cloud/sdk-go-container-registry v1.2.0 h1:C5r2XleKLbSFw9kmb4N8ImqJ/HtLus3yh/R5BHy/6sg=
github.com/ionos-cloud/sdk-go-container-registry v1.2.0/go.mod h1:woBP1+A4N0KXiRj9jG4y/hEXgrVjJv0CUlAvc24mCeo=
github.com/ionos-cloud/sdk-go-dataplatform v1.1.1 h1:Wu9TAiphRyMEweUcQlMblhVCl9qVxQlOYEOw+jJS+Ss=
github.com/ionos-cloud/sdk-go-dataplatform v1.1.1/go.mod h1:3rQrOZFbXcjK3C5Dnjzahsd9x7PO+b4AARdBwdz//Mg=
github.com/ionos-cloud/sdk-go-dbaas-in-memory-db v1.0.1 h1:vd3SCqZ1OI5l7Tzql7NDb5Zlpna8e2SsfEYZRGHOkOo=
Expand Down
20 changes: 10 additions & 10 deletions ionoscloud/data_source_container_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cr "github.com/ionos-cloud/sdk-go-container-registry"
cr "github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
crService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/containerregistry"
Expand Down Expand Up @@ -120,6 +120,7 @@ func dataSourceContainerRegistry() *schema.Resource {
}
}

//nolint:gocyclo
func dataSourceContainerRegistryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).ContainerClient

Expand Down Expand Up @@ -159,22 +160,21 @@ func dataSourceContainerRegistryRead(ctx context.Context, d *schema.ResourceData
return diags
}

results = *registries.Items
results = registries.Items
if nameOk {
partialMatch := d.Get("partial_match").(bool)

log.Printf("[INFO] Using data source for container registry by name with partial_match %t and name: %s", partialMatch, name)

if registries.Items != nil && len(*registries.Items) > 0 {
if registries.Items != nil && len(registries.Items) > 0 {
var registriesByName []cr.RegistryResponse
for _, registryItem := range *registries.Items {
if registryItem.Properties != nil && registryItem.Properties.Name != nil &&
(partialMatch && strings.Contains(*registryItem.Properties.Name, name) ||
!partialMatch && strings.EqualFold(*registryItem.Properties.Name, name)) {
for _, registryItem := range registries.Items {
if partialMatch && strings.Contains(registryItem.Properties.Name, name) ||
!partialMatch && strings.EqualFold(registryItem.Properties.Name, name) {
registriesByName = append(registriesByName, registryItem)
}
}
if registriesByName != nil && len(registriesByName) > 0 {
if len(registriesByName) > 0 {
results = registriesByName
} else {
return diag.FromErr(fmt.Errorf("no registry found with the specified criteria: name = %v", name))
Expand All @@ -185,11 +185,11 @@ func dataSourceContainerRegistryRead(ctx context.Context, d *schema.ResourceData
if locationOk {
var registriesByLocation []cr.RegistryResponse
for _, registryItem := range results {
if registryItem.Properties != nil && registryItem.Properties.Name != nil && strings.EqualFold(*registryItem.Properties.Location, location) {
if strings.EqualFold(registryItem.Properties.Location, location) {
registriesByLocation = append(registriesByLocation, registryItem)
}
}
if registriesByLocation != nil && len(registriesByLocation) > 0 {
if len(registriesByLocation) > 0 {
results = registriesByLocation
} else {
return diag.FromErr(fmt.Errorf("no registry found with the specified criteria: location = %v", location))
Expand Down
18 changes: 7 additions & 11 deletions ionoscloud/data_source_container_registry_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cr "github.com/ionos-cloud/sdk-go-container-registry"
cr "github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
crService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/containerregistry"
Expand Down Expand Up @@ -129,17 +129,16 @@ func dataSourceContainerRegistryTokenRead(ctx context.Context, d *schema.Resourc

log.Printf("[INFO] Using data source for container token by name with partial_match %t and name: %s", partialMatch, name)

if tokens.Items != nil && len(*tokens.Items) > 0 {
for _, tokenItem := range *tokens.Items {
if tokenItem.Properties != nil && tokenItem.Properties.Name != nil &&
(partialMatch && strings.Contains(*tokenItem.Properties.Name, name) ||
!partialMatch && strings.EqualFold(*tokenItem.Properties.Name, name)) {
if tokens.Items != nil && len(tokens.Items) > 0 {
for _, tokenItem := range tokens.Items {
if partialMatch && strings.Contains(tokenItem.Properties.Name, name) ||
!partialMatch && strings.EqualFold(tokenItem.Properties.Name, name) {
results = append(results, tokenItem)
}
}
}

if results == nil || len(results) == 0 {
if len(results) == 0 {
return diag.FromErr(fmt.Errorf("no token found with the specified name = %s", name))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one token found with the specified criteria: name = %s", name))
Expand All @@ -151,11 +150,8 @@ func dataSourceContainerRegistryTokenRead(ctx context.Context, d *schema.Resourc
if token.Id != nil {
d.SetId(*token.Id)
}
if token.Properties == nil {
return diag.FromErr(fmt.Errorf("no token properties found with the specified id = %s", *token.Id))
}

if err := crService.SetTokenData(d, *token.Properties); err != nil {
if err := crService.SetTokenData(d, token.Properties); err != nil {
return diag.FromErr(err)
}

Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/resource_container_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
cr "github.com/ionos-cloud/sdk-go-container-registry"
cr "github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
)
Expand Down
19 changes: 4 additions & 15 deletions ionoscloud/resource_container_registry_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,13 @@ func resourceContainerRegistryTokenCreate(ctx context.Context, d *schema.Resourc

d.SetId(*registryTokenResponse.Id)

if registryTokenResponse.Properties == nil {
return diag.FromErr(fmt.Errorf("no token properties found with the specified id = %s", *registryTokenResponse.Id))
}

if fileStr != "" {
if err := utils.WriteToFile(fileStr, *registryTokenResponse.Properties.Credentials.Password); err != nil {
if err := utils.WriteToFile(fileStr, registryTokenResponse.Properties.Credentials.Password); err != nil {
return diag.FromErr(err)
}
}

if err = crService.SetTokenData(d, *registryTokenResponse.Properties); err != nil {
if err = crService.SetTokenData(d, registryTokenResponse.Properties); err != nil {
return diag.FromErr(err)
}
return nil
Expand All @@ -152,11 +148,7 @@ func resourceContainerRegistryTokenRead(ctx context.Context, d *schema.ResourceD

log.Printf("[INFO] Successfully retrieved registry token %s: %+v", d.Id(), registryToken)

if registryToken.Properties == nil {
return diag.FromErr(fmt.Errorf("no token properties found with the specified id = %s", *registryToken.Id))
}

if err := crService.SetTokenData(d, *registryToken.Properties); err != nil {
if err := crService.SetTokenData(d, registryToken.Properties); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -223,11 +215,8 @@ func resourceContainerRegistryTokenImport(ctx context.Context, d *schema.Resourc
if registryToken.Id != nil {
d.SetId(*registryToken.Id)
}
if registryToken.Properties == nil {
return nil, fmt.Errorf("no token properties found with the specified id = %s", *registryToken.Id)
}

if err := crService.SetTokenData(d, *registryToken.Properties); err != nil {
if err := crService.SetTokenData(d, registryToken.Properties); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/resource_container_registry_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
cr "github.com/ionos-cloud/sdk-go-container-registry"
cr "github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2"
)

func TestAccContainerRegistryTokenBasic(t *testing.T) {
Expand Down
23 changes: 10 additions & 13 deletions services/containerregistry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,33 @@ package containerregistry
import (
"fmt"
"net/http"
"os"
"runtime"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"

"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
cr "github.com/ionos-cloud/sdk-go-container-registry"
cr "github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2"
"github.com/ionos-cloud/sdk-go-bundle/shared"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
)

type Client struct {
sdkClient *cr.APIClient
sdkClient cr.APIClient
}

func NewClient(username, password, token, url, version, terraformVersion string, insecure bool) *Client {
newConfigRegistry := cr.NewConfiguration(username, password, token, url)
newConfigRegistry := shared.NewConfiguration(username, password, token, url)

if os.Getenv(constant.IonosDebug) != "" {
newConfigRegistry.Debug = true
}
newConfigRegistry.MaxRetries = constant.MaxRetries
newConfigRegistry.MaxWaitTime = constant.MaxWaitTime

newConfigRegistry.HTTPClient = &http.Client{Transport: utils.CreateTransport(insecure)}
newConfigRegistry.UserAgent = fmt.Sprintf(
"terraform-provider/%s_ionos-cloud-sdk-go-container-cr/%s_hashicorp-terraform/%s_terraform-plugin-sdk/%s_os/%s_arch/%s",
version, cr.Version, terraformVersion, meta.SDKVersionString(), runtime.GOOS, runtime.GOARCH)

return &Client{
sdkClient: cr.NewAPIClient(newConfigRegistry),
version, cr.Version, terraformVersion, meta.SDKVersionString(), runtime.GOOS, runtime.GOARCH) //nolint:staticcheck
client := &Client{
sdkClient: *cr.NewAPIClient(newConfigRegistry),
}
client.sdkClient.GetConfig().HTTPClient = &http.Client{Transport: utils.CreateTransport(insecure)}

return client
}
7 changes: 4 additions & 3 deletions services/containerregistry/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cr "github.com/ionos-cloud/sdk-go-container-registry"
cr "github.com/ionos-cloud/sdk-go-bundle/products/containerregistry/v2"
"github.com/ionos-cloud/sdk-go-bundle/shared"
)

func (c *Client) GetAllLocations(ctx context.Context) (cr.LocationsResponse, *cr.APIResponse, error) {
func (c *Client) GetAllLocations(ctx context.Context) (cr.LocationsResponse, *shared.APIResponse, error) {
versions, apiResponse, err := c.sdkClient.LocationsApi.LocationsGet(ctx).Execute()
apiResponse.LogInfo()
return versions, apiResponse, err
Expand All @@ -23,7 +24,7 @@ func SetCRLocationsData(d *schema.ResourceData, locations cr.LocationsResponse)

if locations.Items != nil {
var locationList []string
for _, location := range *locations.Items {
for _, location := range locations.Items {
locationList = append(locationList, *location.Id)
}
err := d.Set("locations", locationList)
Expand Down
Loading
Loading