github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/networking/v2/extensions/extensions.go (about)

     1  package extensions
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/tools"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/external"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/security/groups"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/security/rules"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/networks"
    13  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/ports"
    14  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    15  )
    16  
    17  // CreateExternalNetwork will create an external network. An error will be
    18  // returned if the creation failed.
    19  func CreateExternalNetwork(t *testing.T, client *gophercloud.ServiceClient) (*networks.Network, error) {
    20  	networkName := tools.RandomString("TESTACC-", 8)
    21  	networkDescription := tools.RandomString("TESTACC-DESC-", 8)
    22  
    23  	t.Logf("Attempting to create external network: %s", networkName)
    24  
    25  	adminStateUp := true
    26  	isExternal := true
    27  
    28  	networkCreateOpts := networks.CreateOpts{
    29  		Name:         networkName,
    30  		Description:  networkDescription,
    31  		AdminStateUp: &adminStateUp,
    32  	}
    33  
    34  	createOpts := external.CreateOptsExt{
    35  		CreateOptsBuilder: networkCreateOpts,
    36  		External:          &isExternal,
    37  	}
    38  
    39  	network, err := networks.Create(context.TODO(), client, createOpts).Extract()
    40  	if err != nil {
    41  		return network, err
    42  	}
    43  
    44  	t.Logf("Created external network: %s", networkName)
    45  
    46  	th.AssertEquals(t, networkName, network.Name)
    47  	th.AssertEquals(t, networkDescription, network.Description)
    48  
    49  	return network, nil
    50  }
    51  
    52  // CreatePortWithSecurityGroup will create a port with a security group
    53  // attached. An error will be returned if the port could not be created.
    54  func CreatePortWithSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, networkID, subnetID, secGroupID string) (*ports.Port, error) {
    55  	portName := tools.RandomString("TESTACC-", 8)
    56  	portDescription := tools.RandomString("TESTACC-DESC-", 8)
    57  	iFalse := false
    58  
    59  	t.Logf("Attempting to create port: %s", portName)
    60  
    61  	createOpts := ports.CreateOpts{
    62  		NetworkID:      networkID,
    63  		Name:           portName,
    64  		Description:    portDescription,
    65  		AdminStateUp:   &iFalse,
    66  		FixedIPs:       []ports.IP{{SubnetID: subnetID}},
    67  		SecurityGroups: &[]string{secGroupID},
    68  	}
    69  
    70  	port, err := ports.Create(context.TODO(), client, createOpts).Extract()
    71  	if err != nil {
    72  		return port, err
    73  	}
    74  
    75  	t.Logf("Successfully created port: %s", portName)
    76  
    77  	th.AssertEquals(t, portName, port.Name)
    78  	th.AssertEquals(t, portDescription, port.Description)
    79  	th.AssertEquals(t, networkID, port.NetworkID)
    80  
    81  	return port, nil
    82  }
    83  
    84  // CreateSecurityGroup will create a security group with a random name.
    85  // An error will be returned if one was failed to be created.
    86  func CreateSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (*groups.SecGroup, error) {
    87  	secGroupName := tools.RandomString("TESTACC-", 8)
    88  	secGroupDescription := tools.RandomString("TESTACC-DESC-", 8)
    89  
    90  	t.Logf("Attempting to create security group: %s", secGroupName)
    91  
    92  	createOpts := groups.CreateOpts{
    93  		Name:        secGroupName,
    94  		Description: secGroupDescription,
    95  	}
    96  
    97  	secGroup, err := groups.Create(context.TODO(), client, createOpts).Extract()
    98  	if err != nil {
    99  		return secGroup, err
   100  	}
   101  
   102  	t.Logf("Created security group: %s", secGroup.ID)
   103  
   104  	th.AssertEquals(t, secGroupName, secGroup.Name)
   105  	th.AssertEquals(t, secGroupDescription, secGroup.Description)
   106  
   107  	return secGroup, nil
   108  }
   109  
   110  // CreateSecurityGroupRule will create a security group rule with a random name
   111  // and random port between 80 and 99.
   112  // An error will be returned if one was failed to be created.
   113  func CreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) (*rules.SecGroupRule, error) {
   114  	t.Logf("Attempting to create security group rule in group: %s", secGroupID)
   115  
   116  	description := "Rule description"
   117  	fromPort := tools.RandomInt(80, 89)
   118  	toPort := tools.RandomInt(90, 99)
   119  
   120  	createOpts := rules.CreateOpts{
   121  		Description:  description,
   122  		Direction:    "ingress",
   123  		EtherType:    "IPv4",
   124  		SecGroupID:   secGroupID,
   125  		PortRangeMin: fromPort,
   126  		PortRangeMax: toPort,
   127  		Protocol:     rules.ProtocolTCP,
   128  	}
   129  
   130  	rule, err := rules.Create(context.TODO(), client, createOpts).Extract()
   131  	if err != nil {
   132  		return rule, err
   133  	}
   134  
   135  	t.Logf("Created security group rule: %s", rule.ID)
   136  
   137  	th.AssertEquals(t, rule.SecGroupID, secGroupID)
   138  	th.AssertEquals(t, rule.Description, description)
   139  
   140  	return rule, nil
   141  }
   142  
   143  // CreateSecurityGroupRulesBulk will create security group rules with a random name
   144  // and random port between 80 and 99.
   145  // An error will be returned if one was failed to be created.
   146  func CreateSecurityGroupRulesBulk(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) ([]rules.SecGroupRule, error) {
   147  	t.Logf("Attempting to bulk create security group rules in group: %s", secGroupID)
   148  
   149  	sgRulesCreateOpts := make([]rules.CreateOpts, 3)
   150  	for i := range 3 {
   151  		description := "Rule description"
   152  		fromPort := tools.RandomInt(1080, 1089)
   153  		toPort := tools.RandomInt(1090, 1099)
   154  
   155  		sgRulesCreateOpts[i] = rules.CreateOpts{
   156  			Description:  description,
   157  			Direction:    "ingress",
   158  			EtherType:    "IPv4",
   159  			SecGroupID:   secGroupID,
   160  			PortRangeMin: fromPort,
   161  			PortRangeMax: toPort,
   162  			Protocol:     rules.ProtocolTCP,
   163  		}
   164  	}
   165  
   166  	rules, err := rules.CreateBulk(context.TODO(), client, sgRulesCreateOpts).Extract()
   167  	if err != nil {
   168  		return rules, err
   169  	}
   170  
   171  	for i, rule := range rules {
   172  		t.Logf("Created security group rule: %s", rule.ID)
   173  
   174  		th.AssertEquals(t, sgRulesCreateOpts[i].SecGroupID, rule.SecGroupID)
   175  		th.AssertEquals(t, sgRulesCreateOpts[i].Description, rule.Description)
   176  	}
   177  
   178  	return rules, nil
   179  }
   180  
   181  // DeleteSecurityGroup will delete a security group of a specified ID.
   182  // A fatal error will occur if the deletion failed. This works best as a
   183  // deferred function
   184  func DeleteSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, secGroupID string) {
   185  	t.Logf("Attempting to delete security group: %s", secGroupID)
   186  
   187  	err := groups.Delete(context.TODO(), client, secGroupID).ExtractErr()
   188  	if err != nil {
   189  		t.Fatalf("Unable to delete security group: %v", err)
   190  	}
   191  }
   192  
   193  // DeleteSecurityGroupRule will delete a security group rule of a specified ID.
   194  // A fatal error will occur if the deletion failed. This works best as a
   195  // deferred function
   196  func DeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, ruleID string) {
   197  	t.Logf("Attempting to delete security group rule: %s", ruleID)
   198  
   199  	err := rules.Delete(context.TODO(), client, ruleID).ExtractErr()
   200  	if err != nil {
   201  		t.Fatalf("Unable to delete security group rule: %v", err)
   202  	}
   203  }