github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/security_groups/security_groups.go (about)

     1  package security_groups
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  
     7  	"github.com/cloudfoundry/cli/cf/api/resources"
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	"github.com/cloudfoundry/cli/cf/errors"
    10  	"github.com/cloudfoundry/cli/cf/models"
    11  	"github.com/cloudfoundry/cli/cf/net"
    12  )
    13  
    14  type SecurityGroupRepo interface {
    15  	Create(name string, rules []map[string]interface{}) error
    16  	Update(guid string, rules []map[string]interface{}) error
    17  	Read(string) (models.SecurityGroup, error)
    18  	Delete(string) error
    19  	FindAll() ([]models.SecurityGroup, error)
    20  }
    21  
    22  type cloudControllerSecurityGroupRepo struct {
    23  	gateway net.Gateway
    24  	config  core_config.Reader
    25  }
    26  
    27  func NewSecurityGroupRepo(config core_config.Reader, gateway net.Gateway) SecurityGroupRepo {
    28  	return cloudControllerSecurityGroupRepo{
    29  		config:  config,
    30  		gateway: gateway,
    31  	}
    32  }
    33  
    34  func (repo cloudControllerSecurityGroupRepo) Create(name string, rules []map[string]interface{}) error {
    35  	path := "/v2/security_groups"
    36  	params := models.SecurityGroupParams{
    37  		Name:  name,
    38  		Rules: rules,
    39  	}
    40  	return repo.gateway.CreateResourceFromStruct(repo.config.ApiEndpoint(), path, params)
    41  }
    42  
    43  func (repo cloudControllerSecurityGroupRepo) Read(name string) (models.SecurityGroup, error) {
    44  	path := fmt.Sprintf("/v2/security_groups?q=%s&inline-relations-depth=2", url.QueryEscape("name:"+name))
    45  	group := models.SecurityGroup{}
    46  	foundGroup := false
    47  
    48  	err := repo.gateway.ListPaginatedResources(
    49  		repo.config.ApiEndpoint(),
    50  		path,
    51  		resources.SecurityGroupResource{},
    52  		func(resource interface{}) bool {
    53  			if asgr, ok := resource.(resources.SecurityGroupResource); ok {
    54  				group = asgr.ToModel()
    55  				foundGroup = true
    56  			}
    57  
    58  			return false
    59  		},
    60  	)
    61  	if err != nil {
    62  		return group, err
    63  	}
    64  
    65  	if !foundGroup {
    66  		err = errors.NewModelNotFoundError("security group", name)
    67  	}
    68  
    69  	return group, err
    70  }
    71  
    72  func (repo cloudControllerSecurityGroupRepo) Update(guid string, rules []map[string]interface{}) error {
    73  	url := fmt.Sprintf("/v2/security_groups/%s", guid)
    74  	return repo.gateway.UpdateResourceFromStruct(repo.config.ApiEndpoint(), url, models.SecurityGroupParams{Rules: rules})
    75  }
    76  
    77  func (repo cloudControllerSecurityGroupRepo) FindAll() ([]models.SecurityGroup, error) {
    78  	path := "/v2/security_groups?inline-relations-depth=2"
    79  	securityGroups := []models.SecurityGroup{}
    80  
    81  	err := repo.gateway.ListPaginatedResources(
    82  		repo.config.ApiEndpoint(),
    83  		path,
    84  		resources.SecurityGroupResource{},
    85  		func(resource interface{}) bool {
    86  			if securityGroupResource, ok := resource.(resources.SecurityGroupResource); ok {
    87  				securityGroups = append(securityGroups, securityGroupResource.ToModel())
    88  			}
    89  
    90  			return true
    91  		},
    92  	)
    93  
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return securityGroups, err
    99  }
   100  
   101  func (repo cloudControllerSecurityGroupRepo) Delete(securityGroupGuid string) error {
   102  	path := fmt.Sprintf("/v2/security_groups/%s", securityGroupGuid)
   103  	return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path)
   104  }