Skip to content

Commit

Permalink
feat(container) add push image on registry and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Monitob committed Mar 14, 2022
1 parent f4eebf6 commit d5adb0f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 31 deletions.
3 changes: 0 additions & 3 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scaleway
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -506,8 +505,6 @@ func validateDuration() schema.SchemaValidateFunc {

func validateMaxLimit(max uint64) schema.SchemaValidateDiagFunc {
return func(i interface{}, path cty.Path) diag.Diagnostics {
maxPath := path.IndexInt(i.(int))
log.Printf("[DEBUG] SCW Max Limit path: %s: value %v", maxPath, i)
iInt := i.(int)
valueMax := cty.NumberUIntVal(uint64(iInt))
isGreater := valueMax.GreaterThan(cty.NumberUIntVal(max))
Expand Down
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
"scaleway_domain_record": dataSourceScalewayDomainRecord(),
"scaleway_domain_zone": dataSourceScalewayDomainZone(),
"scaleway_container_namespace": dataSourceScalewayContainerNamespace(),
"scaleway_container": dataSourceScalewayContainer(),
"scaleway_function_namespace": dataSourceScalewayFunctionNamespace(),
"scaleway_instance_ip": dataSourceScalewayInstanceIP(),
"scaleway_instance_security_group": dataSourceScalewayInstanceSecurityGroup(),
Expand Down
52 changes: 31 additions & 21 deletions scaleway/resource_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func resourceScalewayContainer() *schema.Resource {
"environment_variables": {
Type: schema.TypeMap,
Optional: true,
Description: "The environment variables to be injected into your container at runtime ",
Description: "The environment variables to be injected into your container at runtime.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 1000),
Expand All @@ -63,31 +63,31 @@ func resourceScalewayContainer() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The minimum of running container instances continuously. Defaults to 0",
Description: "The minimum of running container instances continuously. Defaults to 0.",
},
"max_scale": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The maximum of number of instances this container can scale to. Default to 20",
Description: "The maximum of number of instances this container can scale to. Default to 20.",
},
"memory_limit": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The memory computing resources in MB to allocate to each container. Defaults to 128",
Description: "The memory computing resources in MB to allocate to each container. Defaults to 128.",
},
"cpu_limit": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The amount of vCPU computing resources to allocate to each container. Defaults to 70",
Description: "The amount of vCPU computing resources to allocate to each container. Defaults to 70.",
},
"timeout": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The maximum amount of time in seconds during which your container can process a request before we stop it. Defaults to (300s).",
Description: "The maximum amount of time in seconds during which your container can process a request before we stop it. Defaults to 300s.",
},
"privacy": {
Type: schema.TypeString,
Expand All @@ -104,13 +104,13 @@ func resourceScalewayContainer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The registry image address where your container is stored.",
Description: "The scw registry image address",
},
"max_concurrency": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The maximum the number of simultaneous requests your container can handle at the same time. Defaults to 50",
Description: "The maximum the number of simultaneous requests your container can handle at the same time. Defaults to 50.",
ValidateDiagFunc: validateMaxLimit(containerMaxLimit),
},
"domain_name": {
Expand All @@ -122,7 +122,7 @@ func resourceScalewayContainer() *schema.Resource {
"protocol": {
Type: schema.TypeString,
Optional: true,
Description: "The communication protocol. default (http1)",
Description: "The communication protocol. Defaults to http1.",
Default: container.ContainerProtocolHTTP1.String(),
ValidateFunc: validation.StringInSlice([]string{
container.ContainerProtocolH2c.String(),
Expand All @@ -140,7 +140,7 @@ func resourceScalewayContainer() *schema.Resource {
"redeploy": {
Type: schema.TypeBool,
Optional: true,
Description: "Allow redeploy container",
Description: "Allow deploy container",
Default: false,
},
"region": regionSchema(),
Expand Down Expand Up @@ -195,16 +195,6 @@ func resourceScalewayContainerCreate(ctx context.Context, d *schema.ResourceData

d.SetId(newRegionalIDString(region, res.ID))

return resourceScalewayContainerRead(ctx, d, meta)
}

func resourceScalewayContainerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api, region, containerID, err := containerAPIWithRegionAndID(meta, d.Id())
if err != nil {
return diag.FromErr(err)
}

namespaceID := d.Get("namespace_id")
// verify name space state
_, err = api.WaitForNamespace(&container.WaitForNamespaceRequest{
NamespaceID: expandID(namespaceID),
Expand All @@ -214,7 +204,15 @@ func resourceScalewayContainerRead(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("unexpected namespace error: %s", err)
}

// check for container state
return resourceScalewayContainerRead(ctx, d, meta)
}

func resourceScalewayContainerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api, region, containerID, err := containerAPIWithRegionAndID(meta, d.Id())
if err != nil {
return diag.FromErr(err)
}

co, err := api.WaitForContainer(&container.WaitForContainerRequest{ContainerID: containerID,
Region: region,
}, scw.WithContext(ctx))
Expand Down Expand Up @@ -405,5 +403,17 @@ func resourceScalewayContainerDelete(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}

// check for container state
_, err = api.WaitForContainer(&container.WaitForContainerRequest{ContainerID: containerID,
Region: region,
}, scw.WithContext(ctx))
if err != nil {
if is404Error(err) {
d.SetId("")
return nil
}
return diag.Errorf("unexpected waiting container error: %s", err)
}

return nil
}
11 changes: 11 additions & 0 deletions scaleway/resource_container_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,16 @@ func resourceScalewayContainerNamespaceDelete(ctx context.Context, d *schema.Res
return diag.FromErr(err)
}

_, err = api.WaitForNamespace(&container.WaitForNamespaceRequest{
Region: region,
NamespaceID: id,
}, scw.WithContext(ctx))
if err != nil {
if is404Error(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
return nil
}
59 changes: 52 additions & 7 deletions scaleway/resource_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
"github.com/scaleway/scaleway-sdk-go/api/registry/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/require"
)

// We must add registry for the sweeper
func TestAccScalewayContainer_Basic(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
Expand Down Expand Up @@ -81,6 +81,21 @@ func TestAccScalewayContainer_Basic(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_container.main", "protocol", container.ContainerProtocolHTTP1.String()),
),
},
{
Config: `
resource scaleway_container_namespace main {
}
resource scaleway_container main {
name = "my-container-tf"
namespace_id = scaleway_container_namespace.main.id
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayContainerExists(tt, "scaleway_container.main"),
testRemoveAllRegistryNamespaceGenerated(tt),
),
},
},
})
}
Expand Down Expand Up @@ -169,7 +184,8 @@ func TestAccScalewayContainer_WithIMG(t *testing.T) {
}
}

registryNameSpace := "test-for-container-as-a-service3"
registryNamespace := "test-for-container-as-a-service-02"
containerNamespace := "test-cr-ns-02"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Expand All @@ -184,10 +200,10 @@ func TestAccScalewayContainer_WithIMG(t *testing.T) {
}
resource scaleway_container_namespace main {
name = "test-cr-ns"
name = "%s"
description = "test container"
}
`, registryNameSpace),
`, registryNamespace, containerNamespace),
Check: resource.ComposeTestCheckFunc(
addImageToRegistry(tt, "scaleway_registry_namespace.main"),
),
Expand All @@ -201,7 +217,7 @@ func TestAccScalewayContainer_WithIMG(t *testing.T) {
}
resource scaleway_container_namespace main {
name = "test-cr-ns"
name = "%s"
description = "test container"
}
Expand All @@ -225,11 +241,11 @@ func TestAccScalewayContainer_WithIMG(t *testing.T) {
"foo" = "var"
}
}
`, registryNameSpace),
`, registryNamespace, containerNamespace),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayContainerExists(tt, "scaleway_container.main"),
testCheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
testCheckResourceAttrUUID("scaleway_container.main", "id"),
resource.TestCheckResourceAttrSet("scaleway_container.main", "registry_image"),
resource.TestCheckResourceAttr("scaleway_container.main", "name", "my-container-02"),
resource.TestCheckResourceAttr("scaleway_container.main", "port", "9997"),
resource.TestCheckResourceAttr("scaleway_container.main", "cpu_limit", "140"),
Expand All @@ -246,6 +262,7 @@ func TestAccScalewayContainer_WithIMG(t *testing.T) {
},
})
}

func testAccCheckScalewayContainerExists(tt *TestTools, n string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[n]
Expand Down Expand Up @@ -300,3 +317,31 @@ func testAccCheckScalewayContainerDestroy(tt *TestTools) resource.TestCheckFunc
return nil
}
}

func testRemoveAllRegistryNamespaceGenerated(tt *TestTools) resource.TestCheckFunc {
return func(s *terraform.State) error {
return sweepRegions([]scw.Region{scw.RegionFrPar, scw.RegionNlAms}, func(scwClient *scw.Client, region scw.Region) error {
meta := tt.Meta
registryAPI := registry.NewAPI(meta.scwClient)
listNamespaces, err := registryAPI.ListNamespaces(
&registry.ListNamespacesRequest{Region: region}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error listing namespaces in (%s) in test: %s", region, err)
}

for _, ns := range listNamespaces.Namespaces {
_, err := registryAPI.DeleteNamespace(&registry.DeleteNamespaceRequest{
NamespaceID: ns.ID,
Region: region,
})
if err != nil {
l.Debugf("delete: error (%s)", err)

return fmt.Errorf("error deleting namespace in sweeper: %s", err)
}
}

return nil
})
}
}

0 comments on commit d5adb0f

Please sign in to comment.