github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+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", 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 return group, errors.NewModelNotFoundError("security group", name) 67 } 68 69 err = repo.gateway.ListPaginatedResources( 70 repo.config.ApiEndpoint(), 71 group.SpaceUrl+"?inline-relations-depth=1", 72 resources.SpaceResource{}, 73 func(resource interface{}) bool { 74 if asgr, ok := resource.(resources.SpaceResource); ok { 75 group.Spaces = append(group.Spaces, asgr.ToModel()) 76 return true 77 } 78 return false 79 }, 80 ) 81 82 return group, err 83 } 84 85 func (repo cloudControllerSecurityGroupRepo) Update(guid string, rules []map[string]interface{}) error { 86 url := fmt.Sprintf("/v2/security_groups/%s", guid) 87 return repo.gateway.UpdateResourceFromStruct(repo.config.ApiEndpoint(), url, models.SecurityGroupParams{Rules: rules}) 88 } 89 90 func (repo cloudControllerSecurityGroupRepo) FindAll() ([]models.SecurityGroup, error) { 91 path := "/v2/security_groups" 92 securityGroups := []models.SecurityGroup{} 93 94 err := repo.gateway.ListPaginatedResources( 95 repo.config.ApiEndpoint(), 96 path, 97 resources.SecurityGroupResource{}, 98 func(resource interface{}) bool { 99 if securityGroupResource, ok := resource.(resources.SecurityGroupResource); ok { 100 securityGroups = append(securityGroups, securityGroupResource.ToModel()) 101 } 102 103 return true 104 }, 105 ) 106 107 if err != nil { 108 return nil, err 109 } 110 111 for i, _ := range securityGroups { 112 err = repo.gateway.ListPaginatedResources( 113 repo.config.ApiEndpoint(), 114 securityGroups[i].SpaceUrl+"?inline-relations-depth=1", 115 resources.SpaceResource{}, 116 func(resource interface{}) bool { 117 if asgr, ok := resource.(resources.SpaceResource); ok { 118 securityGroups[i].Spaces = append(securityGroups[i].Spaces, asgr.ToModel()) 119 return true 120 } 121 return false 122 }, 123 ) 124 } 125 126 return securityGroups, err 127 } 128 129 func (repo cloudControllerSecurityGroupRepo) Delete(securityGroupGuid string) error { 130 path := fmt.Sprintf("/v2/security_groups/%s", securityGroupGuid) 131 return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path) 132 }