@@ -17,6 +17,8 @@ package cloud
1717import (
1818 "context"
1919 "fmt"
20+ cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
21+ cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
2022 "strings"
2123 "time"
2224
@@ -25,9 +27,6 @@ import (
2527 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2628 apierrors "k8s.io/apimachinery/pkg/api/errors"
2729 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
29- cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
30- cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
3130)
3231
3332func resourceCloudEnvironment () * schema.Resource {
@@ -56,9 +55,10 @@ func resourceCloudEnvironment() *schema.Resource {
5655 diff .HasChanges ("cloud_connection_name" ) ||
5756 diff .HasChanges ("region" ) ||
5857 diff .HasChanges ("network_id" ) ||
59- diff .HasChanges ("network_cidr" ) {
58+ diff .HasChanges ("network_cidr" ) ||
59+ diff .HasChanges ("network_subnet_cidr" ) {
6060 return fmt .Errorf ("ERROR_UPDATE_CLOUD_ENVIRONMENT: " +
61- "The cloud environment does not support updates on the attributes: organization, cloud_connection_name, region, network_id, network_cidr. Please recreate it" )
61+ "The cloud environment does not support updates on the attributes: organization, cloud_connection_name, region, network_id, network_cidr, network_subnet_cidr . Please recreate it" )
6262 }
6363 return nil
6464 },
@@ -120,6 +120,11 @@ func resourceCloudEnvironment() *schema.Resource {
120120 Optional : true ,
121121 ValidateFunc : validateCidrRange ,
122122 },
123+ "subnet_cidr" : {
124+ Type : schema .TypeString ,
125+ Optional : true ,
126+ ValidateFunc : validateCidrRange ,
127+ },
123128 },
124129 },
125130 },
@@ -260,12 +265,27 @@ func resourceCloudEnvironmentCreate(ctx context.Context, d *schema.ResourceData,
260265 networkCidr := networkItemMap ["cidr" ].(string )
261266 cloudEnvironment .Spec .Network .CIDR = networkCidr
262267 }
268+ if networkItemMap ["subnet_cidr" ] != nil {
269+ subnetCidr := networkItemMap ["subnet_cidr" ].(string )
270+ cloudEnvironment .Spec .Network .SubnetCIDR = subnetCidr
271+ }
263272 }
264273 }
265274
266275 if cloudEnvironment .Spec .Network .ID == "" && cloudEnvironment .Spec .Network .CIDR == "" {
267276 return diag .FromErr (fmt .Errorf ("ERROR_CREATE_CLOUD_ENVIRONMENT: " + "One of network.id or network.cidr must be set" ))
268277 }
278+ if cc .Spec .ConnectionType == cloudv1alpha1 .ConnectionTypeAzure {
279+ if cloudEnvironment .Spec .Network .CIDR != "" {
280+ if cloudEnvironment .Spec .Network .SubnetCIDR == "" {
281+ cloudEnvironment .Spec .Network .SubnetCIDR = cloudEnvironment .Spec .Network .CIDR
282+ }
283+ if validate , _ := validateSubnetCIDR (cloudEnvironment .Spec .Network .SubnetCIDR , cloudEnvironment .Spec .Network .CIDR ); ! validate {
284+ return diag .FromErr (fmt .Errorf ("ERROR_CREATE_CLOUD_ENVIRONMENT: " +
285+ "Azure cloud environment requires network.subnet_cidr to be a subnet of network.cidr" ))
286+ }
287+ }
288+ }
269289
270290 expandDns := func () error {
271291 for _ , l := range dns {
@@ -393,9 +413,10 @@ func resourceCloudEnvironmentUpdate(ctx context.Context, d *schema.ResourceData,
393413 d .HasChanges ("cloud_connection_name" ) ||
394414 d .HasChanges ("region" ) ||
395415 d .HasChanges ("network_id" ) ||
396- d .HasChanges ("network_cidr" ) {
416+ d .HasChanges ("network_cidr" ) ||
417+ d .HasChanges ("network_subnet_cidr" ) {
397418 return diag .FromErr (fmt .Errorf ("ERROR_UPDATE_CLOUD_ENVIRONMENT: " +
398- "The cloud environment does not support updates on the attributes: organization, cloud_connection_name, region, network_id, network_cidr. Please recreate it" ))
419+ "The cloud environment does not support updates on the attributes: organization, cloud_connection_name, region, network_id, network_cidr, network_subnet_cidr . Please recreate it" ))
399420 }
400421
401422 cloudEnvironment , err := clientSet .CloudV1alpha1 ().CloudEnvironments (namespace ).Get (ctx , name , metav1.GetOptions {})
0 commit comments