github.com/sleungcy/cli@v7.1.0+incompatible/integration/helpers/security_group.go (about)

     1  package helpers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"code.cloudfoundry.org/cli/resources"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  // NewSecurityGroup returns a new security group with the given attributes
    16  func NewSecurityGroup(name string, protocol string, destination string, ports *string, description *string) resources.SecurityGroup {
    17  	return resources.SecurityGroup{
    18  		Name: name,
    19  		Rules: []resources.Rule{{
    20  			Protocol:    protocol,
    21  			Destination: destination,
    22  			Ports:       ports,
    23  			Description: description,
    24  		}},
    25  	}
    26  }
    27  
    28  // CreateSecurityGroup Creates a new security group on the API using the 'cf create-security-group'
    29  func CreateSecurityGroup(s resources.SecurityGroup) {
    30  	dir, err := ioutil.TempDir("", "simple-security-group")
    31  	Expect(err).ToNot(HaveOccurred())
    32  	defer os.RemoveAll(dir)
    33  
    34  	tempfile := filepath.Join(dir, "security-group.json")
    35  
    36  	securityGroup, err := json.Marshal(s.Rules)
    37  	Expect(err).ToNot(HaveOccurred())
    38  
    39  	err = ioutil.WriteFile(tempfile, securityGroup, 0666)
    40  	Expect(err).ToNot(HaveOccurred())
    41  	Eventually(CF("create-security-group", s.Name, tempfile)).Should(Exit(0))
    42  }
    43  
    44  // DeleteSecurityGroup Deletes a security group on the API using the 'cf delete-security-group'
    45  func DeleteSecurityGroup(s resources.SecurityGroup) {
    46  	if s.Name == "" {
    47  		fmt.Println("Empty security group name. Skipping deletion.")
    48  		return
    49  	}
    50  	Eventually(CF("delete-security-group", s.Name, "-f")).Should(Exit(0))
    51  }