github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/securitygroups/security_groups.go (about)

     1  package securitygroups
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/resources"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/net"
    12  )
    13  
    14  //go:generate counterfeiter . SecurityGroupRepo
    15  
    16  type SecurityGroupRepo interface {
    17  	Create(name string, rules []map[string]interface{}) error
    18  	Update(guid string, rules []map[string]interface{}) error
    19  	Read(string) (models.SecurityGroup, error)
    20  	Delete(string) error
    21  	FindAll() ([]models.SecurityGroup, error)
    22  }
    23  
    24  type cloudControllerSecurityGroupRepo struct {
    25  	gateway net.Gateway
    26  	config  coreconfig.Reader
    27  }
    28  
    29  func NewSecurityGroupRepo(config coreconfig.Reader, gateway net.Gateway) SecurityGroupRepo {
    30  	return cloudControllerSecurityGroupRepo{
    31  		config:  config,
    32  		gateway: gateway,
    33  	}
    34  }
    35  
    36  func (repo cloudControllerSecurityGroupRepo) Create(name string, rules []map[string]interface{}) error {
    37  	path := "/v2/security_groups"
    38  	params := models.SecurityGroupParams{
    39  		Name:  name,
    40  		Rules: rules,
    41  	}
    42  	return repo.gateway.CreateResourceFromStruct(repo.config.APIEndpoint(), path, params)
    43  }
    44  
    45  func (repo cloudControllerSecurityGroupRepo) Read(name string) (models.SecurityGroup, error) {
    46  	path := fmt.Sprintf("/v2/security_groups?q=%s", url.QueryEscape("name:"+name))
    47  	group := models.SecurityGroup{}
    48  	foundGroup := false
    49  
    50  	err := repo.gateway.ListPaginatedResources(
    51  		repo.config.APIEndpoint(),
    52  		path,
    53  		resources.SecurityGroupResource{},
    54  		func(resource interface{}) bool {
    55  			if asgr, ok := resource.(resources.SecurityGroupResource); ok {
    56  				group = asgr.ToModel()
    57  				foundGroup = true
    58  			}
    59  
    60  			return false
    61  		},
    62  	)
    63  	if err != nil {
    64  		return group, err
    65  	}
    66  
    67  	if !foundGroup {
    68  		return group, errors.NewModelNotFoundError("security group", name)
    69  	}
    70  
    71  	err = repo.gateway.ListPaginatedResources(
    72  		repo.config.APIEndpoint(),
    73  		group.SpaceURL+"?inline-relations-depth=1",
    74  		resources.SpaceResource{},
    75  		func(resource interface{}) bool {
    76  			if asgr, ok := resource.(resources.SpaceResource); ok {
    77  				group.Spaces = append(group.Spaces, asgr.ToModel())
    78  				return true
    79  			}
    80  			return false
    81  		},
    82  	)
    83  
    84  	return group, err
    85  }
    86  
    87  func (repo cloudControllerSecurityGroupRepo) Update(guid string, rules []map[string]interface{}) error {
    88  	url := fmt.Sprintf("/v2/security_groups/%s", guid)
    89  	return repo.gateway.UpdateResourceFromStruct(repo.config.APIEndpoint(), url, models.SecurityGroupParams{Rules: rules})
    90  }
    91  
    92  func (repo cloudControllerSecurityGroupRepo) FindAll() ([]models.SecurityGroup, error) {
    93  	path := "/v2/security_groups"
    94  	securityGroups := []models.SecurityGroup{}
    95  
    96  	err := repo.gateway.ListPaginatedResources(
    97  		repo.config.APIEndpoint(),
    98  		path,
    99  		resources.SecurityGroupResource{},
   100  		func(resource interface{}) bool {
   101  			if securityGroupResource, ok := resource.(resources.SecurityGroupResource); ok {
   102  				securityGroups = append(securityGroups, securityGroupResource.ToModel())
   103  			}
   104  
   105  			return true
   106  		},
   107  	)
   108  
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	for i := range securityGroups {
   114  		err = repo.gateway.ListPaginatedResources(
   115  			repo.config.APIEndpoint(),
   116  			securityGroups[i].SpaceURL+"?inline-relations-depth=1",
   117  			resources.SpaceResource{},
   118  			func(resource interface{}) bool {
   119  				if asgr, ok := resource.(resources.SpaceResource); ok {
   120  					securityGroups[i].Spaces = append(securityGroups[i].Spaces, asgr.ToModel())
   121  					return true
   122  				}
   123  				return false
   124  			},
   125  		)
   126  	}
   127  
   128  	return securityGroups, err
   129  }
   130  
   131  func (repo cloudControllerSecurityGroupRepo) Delete(securityGroupGUID string) error {
   132  	path := fmt.Sprintf("/v2/security_groups/%s", securityGroupGUID)
   133  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
   134  }