github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/security_group_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	. "code.cloudfoundry.org/cli/actor/v2action"
     9  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    12  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Security Group Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    26  	})
    27  
    28  	Describe("GetSecurityGroupsWithOrganizationSpaceAndLifecycle", func() {
    29  		var (
    30  			secGroupOrgSpaces []SecurityGroupWithOrganizationSpaceAndLifecycle
    31  			includeStaging    bool
    32  			warnings          Warnings
    33  			err               error
    34  		)
    35  
    36  		JustBeforeEach(func() {
    37  			secGroupOrgSpaces, warnings, err = actor.GetSecurityGroupsWithOrganizationSpaceAndLifecycle(includeStaging)
    38  		})
    39  
    40  		When("an error occurs getting security groups", func() {
    41  			var returnedError error
    42  
    43  			BeforeEach(func() {
    44  				returnedError = errors.New("get-security-groups-error")
    45  				fakeCloudControllerClient.GetSecurityGroupsReturns(
    46  					nil,
    47  					ccv2.Warnings{"warning-1", "warning-2"},
    48  					returnedError,
    49  				)
    50  			})
    51  
    52  			It("returns the error and all warnings", func() {
    53  				Expect(err).To(MatchError(returnedError))
    54  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    55  			})
    56  		})
    57  
    58  		When("an error occurs getting running spaces", func() {
    59  			var returnedError error
    60  
    61  			BeforeEach(func() {
    62  				fakeCloudControllerClient.GetSecurityGroupsReturns(
    63  					[]ccv2.SecurityGroup{
    64  						{
    65  							GUID: "security-group-guid-1",
    66  							Name: "security-group-1",
    67  						},
    68  					},
    69  					ccv2.Warnings{"warning-1", "warning-2"},
    70  					nil,
    71  				)
    72  			})
    73  
    74  			When("the error is generic", func() {
    75  				BeforeEach(func() {
    76  					returnedError = errors.New("get-spaces-error")
    77  					fakeCloudControllerClient.GetSecurityGroupSpacesReturns(
    78  						nil,
    79  						ccv2.Warnings{"warning-3", "warning-4"},
    80  						returnedError,
    81  					)
    82  				})
    83  
    84  				It("returns the error and all warnings", func() {
    85  					Expect(err).To(MatchError(returnedError))
    86  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4"))
    87  					Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
    88  					Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(BeNil())
    89  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesCallCount()).To(Equal(1))
    90  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
    91  				})
    92  			})
    93  
    94  			When("the error is a resource not found error", func() {
    95  				BeforeEach(func() {
    96  					returnedError = ccerror.ResourceNotFoundError{Message: "could not find security group"}
    97  					fakeCloudControllerClient.GetSecurityGroupSpacesReturns(
    98  						nil,
    99  						ccv2.Warnings{"warning-3", "warning-4"},
   100  						returnedError,
   101  					)
   102  				})
   103  
   104  				It("returns all warnings and continues", func() {
   105  					Expect(err).ToNot(HaveOccurred())
   106  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", returnedError.Error()))
   107  				})
   108  			})
   109  		})
   110  
   111  		When("an error occurs getting staging spaces", func() {
   112  			var returnedError error
   113  
   114  			BeforeEach(func() {
   115  				includeStaging = true
   116  
   117  				fakeCloudControllerClient.GetSecurityGroupsReturns(
   118  					[]ccv2.SecurityGroup{
   119  						{
   120  							GUID: "security-group-guid-1",
   121  							Name: "security-group-1",
   122  						},
   123  					},
   124  					ccv2.Warnings{"warning-1", "warning-2"},
   125  					nil,
   126  				)
   127  
   128  				fakeCloudControllerClient.GetSecurityGroupSpacesReturns(
   129  					nil,
   130  					ccv2.Warnings{"warning-3", "warning-4"},
   131  					nil,
   132  				)
   133  			})
   134  
   135  			When("the error is generic", func() {
   136  				BeforeEach(func() {
   137  					returnedError = errors.New("get-staging-spaces-error")
   138  					fakeCloudControllerClient.GetSecurityGroupStagingSpacesReturns(
   139  						nil,
   140  						ccv2.Warnings{"warning-5", "warning-6"},
   141  						returnedError,
   142  					)
   143  				})
   144  
   145  				It("returns the error and all warnings", func() {
   146  					Expect(err).To(MatchError(returnedError))
   147  					Expect(warnings).To(ConsistOf(
   148  						"warning-1",
   149  						"warning-2",
   150  						"warning-3",
   151  						"warning-4",
   152  						"warning-5",
   153  						"warning-6",
   154  					))
   155  					Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
   156  					Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(BeNil())
   157  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesCallCount()).To(Equal(1))
   158  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
   159  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesCallCount()).To(Equal(1))
   160  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
   161  				})
   162  			})
   163  
   164  			When("the error is a resource not found error", func() {
   165  				BeforeEach(func() {
   166  					returnedError = ccerror.ResourceNotFoundError{Message: "could not find security group"}
   167  					fakeCloudControllerClient.GetSecurityGroupStagingSpacesReturns(
   168  						nil,
   169  						ccv2.Warnings{"warning-5", "warning-6"},
   170  						returnedError,
   171  					)
   172  				})
   173  
   174  				It("returns all warnings and continues", func() {
   175  					Expect(err).ToNot(HaveOccurred())
   176  					Expect(warnings).To(ConsistOf(
   177  						"warning-1",
   178  						"warning-2",
   179  						"warning-3",
   180  						"warning-4",
   181  						"warning-5",
   182  						"warning-6",
   183  						returnedError.Error()))
   184  				})
   185  			})
   186  		})
   187  
   188  		When("an error occurs getting an organization", func() {
   189  			var returnedError error
   190  
   191  			BeforeEach(func() {
   192  				includeStaging = true
   193  
   194  				fakeCloudControllerClient.GetSecurityGroupsReturns(
   195  					[]ccv2.SecurityGroup{
   196  						{
   197  							GUID: "security-group-guid-1",
   198  							Name: "security-group-1",
   199  						},
   200  					},
   201  					ccv2.Warnings{"warning-1", "warning-2"},
   202  					nil,
   203  				)
   204  				fakeCloudControllerClient.GetSecurityGroupSpacesReturns(
   205  					[]ccv2.Space{
   206  						{
   207  							GUID:             "space-guid-11",
   208  							Name:             "space-11",
   209  							OrganizationGUID: "org-guid-11",
   210  						},
   211  					},
   212  					ccv2.Warnings{"warning-3", "warning-4"},
   213  					nil,
   214  				)
   215  				fakeCloudControllerClient.GetSecurityGroupStagingSpacesReturns(
   216  					[]ccv2.Space{
   217  						{
   218  							GUID:             "space-guid-12",
   219  							Name:             "space-12",
   220  							OrganizationGUID: "org-guid-12",
   221  						},
   222  					},
   223  					ccv2.Warnings{"warning-5", "warning-6"},
   224  					nil,
   225  				)
   226  			})
   227  
   228  			When("the error is generic", func() {
   229  				BeforeEach(func() {
   230  					returnedError = errors.New("get-org-error")
   231  					fakeCloudControllerClient.GetOrganizationReturns(
   232  						ccv2.Organization{},
   233  						ccv2.Warnings{"warning-7", "warning-8"},
   234  						returnedError,
   235  					)
   236  				})
   237  
   238  				It("returns the error and all warnings", func() {
   239  					Expect(err).To(MatchError(returnedError))
   240  					Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6", "warning-7", "warning-8"))
   241  					Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
   242  					Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(BeNil())
   243  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesCallCount()).To(Equal(1))
   244  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
   245  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesCallCount()).To(Equal(1))
   246  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
   247  					Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(1))
   248  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(0)).To(Equal("org-guid-11"))
   249  				})
   250  			})
   251  
   252  			When("the error is a resource not found error", func() {
   253  				BeforeEach(func() {
   254  					returnedError = ccerror.ResourceNotFoundError{Message: "could not find the org"}
   255  					fakeCloudControllerClient.GetOrganizationReturnsOnCall(0,
   256  						ccv2.Organization{},
   257  						ccv2.Warnings{"warning-7", "warning-8"},
   258  						returnedError,
   259  					)
   260  					fakeCloudControllerClient.GetOrganizationReturnsOnCall(1,
   261  						ccv2.Organization{},
   262  						ccv2.Warnings{"warning-9", "warning-10"},
   263  						nil,
   264  					)
   265  				})
   266  
   267  				It("returns all warnings and continues", func() {
   268  					Expect(err).ToNot(HaveOccurred())
   269  					Expect(warnings).To(BeEquivalentTo(Warnings{
   270  						"warning-1", "warning-2",
   271  						"warning-3", "warning-4",
   272  						"warning-5", "warning-6",
   273  						"warning-7", "warning-8",
   274  						returnedError.Error(),
   275  						"warning-9", "warning-10",
   276  					}))
   277  					Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(2))
   278  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(0)).To(Equal("org-guid-11"))
   279  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(1)).To(Equal("org-guid-12"))
   280  				})
   281  			})
   282  		})
   283  
   284  		When("no errors are encountered", func() {
   285  			var (
   286  				expectedSecurityGroup1 SecurityGroup
   287  				expectedSecurityGroup2 SecurityGroup
   288  				expectedSecurityGroup3 SecurityGroup
   289  				expectedSecurityGroup4 SecurityGroup
   290  				expectedSecurityGroup5 SecurityGroup
   291  				expectedSecurityGroup6 SecurityGroup
   292  				expectedSecurityGroup7 SecurityGroup
   293  
   294  				expectedOrg11  Organization
   295  				expectedOrg12  Organization
   296  				expectedOrg13  Organization
   297  				expectedOrg21  Organization
   298  				expectedOrg23  Organization
   299  				expectedOrg33  Organization
   300  				expectedOrgAll Organization
   301  
   302  				expectedSpace11  Space
   303  				expectedSpace12  Space
   304  				expectedSpace13  Space
   305  				expectedSpace21  Space
   306  				expectedSpace22  Space
   307  				expectedSpace23  Space
   308  				expectedSpace31  Space
   309  				expectedSpace32  Space
   310  				expectedSpace33  Space
   311  				expectedSpaceAll Space
   312  			)
   313  
   314  			BeforeEach(func() {
   315  				expectedSecurityGroup1 = SecurityGroup{
   316  					GUID:           "security-group-guid-1",
   317  					Name:           "security-group-1",
   318  					RunningDefault: true,
   319  				}
   320  				expectedSecurityGroup2 = SecurityGroup{
   321  					GUID:           "security-group-guid-2",
   322  					Name:           "security-group-2",
   323  					StagingDefault: true,
   324  				}
   325  				expectedSecurityGroup3 = SecurityGroup{
   326  					GUID: "security-group-guid-3",
   327  					Name: "security-group-3",
   328  				}
   329  				expectedSecurityGroup4 = SecurityGroup{
   330  					GUID: "security-group-guid-4",
   331  					Name: "security-group-4",
   332  				}
   333  				expectedSecurityGroup5 = SecurityGroup{
   334  					GUID:           "security-group-guid-5",
   335  					Name:           "security-group-5",
   336  					RunningDefault: true,
   337  				}
   338  				expectedSecurityGroup6 = SecurityGroup{
   339  					GUID:           "security-group-guid-6",
   340  					Name:           "security-group-6",
   341  					StagingDefault: true,
   342  				}
   343  				expectedSecurityGroup7 = SecurityGroup{
   344  					GUID:           "security-group-guid-7",
   345  					Name:           "security-group-7",
   346  					RunningDefault: true,
   347  					StagingDefault: true,
   348  				}
   349  
   350  				expectedOrg11 = Organization{
   351  					GUID: "<<org-guid-11",
   352  					Name: "org-11",
   353  				}
   354  				expectedOrg12 = Organization{
   355  					GUID: "org-guid-12",
   356  					Name: "org-12",
   357  				}
   358  				expectedOrg13 = Organization{
   359  					GUID: "org-guid-13",
   360  					Name: "org-13",
   361  				}
   362  				expectedOrg21 = Organization{
   363  					GUID: "org-guid-21",
   364  					Name: "org-21",
   365  				}
   366  				expectedOrg23 = Organization{
   367  					GUID: "org-guid-23",
   368  					Name: "org-23",
   369  				}
   370  				expectedOrg33 = Organization{
   371  					GUID: "org-guid-33",
   372  					Name: "org-33",
   373  				}
   374  				expectedOrgAll = Organization{
   375  					Name: "",
   376  				}
   377  
   378  				expectedSpace11 = Space{
   379  					GUID: "space-guid-11",
   380  					Name: "space-11",
   381  				}
   382  				expectedSpace12 = Space{
   383  					GUID: "space-guid-12",
   384  					Name: "space-12",
   385  				}
   386  				expectedSpace13 = Space{
   387  					GUID: "space-guid-13",
   388  					Name: "space-13",
   389  				}
   390  				expectedSpace21 = Space{
   391  					GUID: "space-guid-21",
   392  					Name: "space-21",
   393  				}
   394  				expectedSpace22 = Space{
   395  					GUID: "space-guid-22",
   396  					Name: "space-22",
   397  				}
   398  				expectedSpace23 = Space{
   399  					GUID: "space-guid-23",
   400  					Name: "space-23",
   401  				}
   402  				expectedSpace31 = Space{
   403  					GUID: "space-guid-31",
   404  					Name: "space-31",
   405  				}
   406  				expectedSpace32 = Space{
   407  					GUID: "space-guid-32",
   408  					Name: "space-32",
   409  				}
   410  				expectedSpace33 = Space{
   411  					GUID: "space-guid-33",
   412  					Name: "space-33",
   413  				}
   414  				expectedSpaceAll = Space{
   415  					Name: "",
   416  				}
   417  
   418  				fakeCloudControllerClient.GetSecurityGroupsReturns(
   419  					[]ccv2.SecurityGroup{
   420  						{
   421  							GUID:           "security-group-guid-1",
   422  							Name:           "security-group-1",
   423  							RunningDefault: true,
   424  						},
   425  						{
   426  							GUID:           "security-group-guid-2",
   427  							Name:           "security-group-2",
   428  							StagingDefault: true,
   429  						},
   430  						{
   431  							GUID: "security-group-guid-3",
   432  							Name: "security-group-3",
   433  						},
   434  						{
   435  							GUID: "security-group-guid-4",
   436  							Name: "security-group-4",
   437  						},
   438  						{
   439  							GUID:           "security-group-guid-5",
   440  							Name:           "security-group-5",
   441  							RunningDefault: true,
   442  						},
   443  						{
   444  							GUID:           "security-group-guid-6",
   445  							Name:           "security-group-6",
   446  							StagingDefault: true,
   447  						},
   448  						{
   449  							GUID:           "security-group-guid-7",
   450  							Name:           "security-group-7",
   451  							RunningDefault: true,
   452  							StagingDefault: true,
   453  						},
   454  					},
   455  					ccv2.Warnings{"warning-1", "warning-2"},
   456  					nil,
   457  				)
   458  
   459  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(0,
   460  					[]ccv2.Space{
   461  						{
   462  							GUID:             "space-guid-13",
   463  							Name:             "space-13",
   464  							OrganizationGUID: "org-guid-13",
   465  						},
   466  						{
   467  							GUID:             "space-guid-12",
   468  							Name:             "space-12",
   469  							OrganizationGUID: "org-guid-12",
   470  						},
   471  						{
   472  							GUID:             "space-guid-11",
   473  							Name:             "space-11",
   474  							OrganizationGUID: "<<org-guid-11",
   475  						},
   476  					},
   477  					ccv2.Warnings{"warning-3", "warning-4"},
   478  					nil,
   479  				)
   480  
   481  				fakeCloudControllerClient.GetSecurityGroupStagingSpacesReturnsOnCall(0,
   482  					[]ccv2.Space{
   483  						{
   484  							GUID:             "space-guid-13",
   485  							Name:             "space-13",
   486  							OrganizationGUID: "org-guid-13",
   487  						},
   488  						{
   489  							GUID:             "space-guid-12",
   490  							Name:             "space-12",
   491  							OrganizationGUID: "org-guid-12",
   492  						},
   493  						{
   494  							GUID:             "space-guid-11",
   495  							Name:             "space-11",
   496  							OrganizationGUID: "<<org-guid-11",
   497  						},
   498  					},
   499  					ccv2.Warnings{"warning-5", "warning-6"},
   500  					nil,
   501  				)
   502  
   503  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(1,
   504  					[]ccv2.Space{
   505  						{
   506  							GUID:             "space-guid-21",
   507  							Name:             "space-21",
   508  							OrganizationGUID: "org-guid-21",
   509  						},
   510  						{
   511  							GUID:             "space-guid-23",
   512  							Name:             "space-23",
   513  							OrganizationGUID: "org-guid-23",
   514  						},
   515  						{
   516  							GUID:             "space-guid-22",
   517  							Name:             "space-22",
   518  							OrganizationGUID: "<<org-guid-11",
   519  						},
   520  					},
   521  					ccv2.Warnings{"warning-7", "warning-8"},
   522  					nil,
   523  				)
   524  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(2,
   525  					[]ccv2.Space{},
   526  					ccv2.Warnings{"warning-9", "warning-10"},
   527  					nil,
   528  				)
   529  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(3,
   530  					[]ccv2.Space{
   531  						{
   532  							GUID:             "space-guid-31",
   533  							Name:             "space-31",
   534  							OrganizationGUID: "org-guid-23",
   535  						},
   536  						{
   537  							GUID:             "space-guid-32",
   538  							Name:             "space-32",
   539  							OrganizationGUID: "<<org-guid-11",
   540  						},
   541  						{
   542  							GUID:             "space-guid-33",
   543  							Name:             "space-33",
   544  							OrganizationGUID: "org-guid-33",
   545  						},
   546  					},
   547  					ccv2.Warnings{"warning-11", "warning-12"},
   548  					nil,
   549  				)
   550  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(4,
   551  					[]ccv2.Space{},
   552  					ccv2.Warnings{"warning-31", "warning-32"},
   553  					nil,
   554  				)
   555  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(5,
   556  					[]ccv2.Space{},
   557  					ccv2.Warnings{"warning-33", "warning-34"},
   558  					nil,
   559  				)
   560  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(6,
   561  					[]ccv2.Space{},
   562  					ccv2.Warnings{"warning-35", "warning-36"},
   563  					nil,
   564  				)
   565  				fakeCloudControllerClient.GetOrganizationReturnsOnCall(0,
   566  					ccv2.Organization{
   567  						GUID: "org-guid-13",
   568  						Name: "org-13",
   569  					},
   570  					ccv2.Warnings{"warning-13", "warning-14"},
   571  					nil,
   572  				)
   573  				fakeCloudControllerClient.GetOrganizationReturnsOnCall(1,
   574  					ccv2.Organization{
   575  						GUID: "org-guid-12",
   576  						Name: "org-12",
   577  					},
   578  					ccv2.Warnings{"warning-15", "warning-16"},
   579  					nil,
   580  				)
   581  				fakeCloudControllerClient.GetOrganizationReturnsOnCall(2,
   582  					ccv2.Organization{
   583  						GUID: "<<org-guid-11",
   584  						Name: "org-11",
   585  					},
   586  					ccv2.Warnings{"warning-17", "warning-18"},
   587  					nil,
   588  				)
   589  				fakeCloudControllerClient.GetOrganizationReturnsOnCall(3,
   590  					ccv2.Organization{
   591  						GUID: "org-guid-21",
   592  						Name: "org-21",
   593  					},
   594  					ccv2.Warnings{"warning-19", "warning-20"},
   595  					nil,
   596  				)
   597  				fakeCloudControllerClient.GetOrganizationReturnsOnCall(4,
   598  					ccv2.Organization{
   599  						GUID: "org-guid-23",
   600  						Name: "org-23",
   601  					},
   602  					ccv2.Warnings{"warning-21", "warning-22"},
   603  					nil,
   604  				)
   605  				fakeCloudControllerClient.GetOrganizationReturnsOnCall(5,
   606  					ccv2.Organization{
   607  						GUID: "org-guid-33",
   608  						Name: "org-33",
   609  					},
   610  					ccv2.Warnings{"warning-25", "warning-26"},
   611  					nil,
   612  				)
   613  			})
   614  
   615  			When("security groups bound to spaces in the staging lifecycle are included", func() {
   616  				BeforeEach(func() {
   617  					includeStaging = true
   618  				})
   619  
   620  				It("returns a slice of SecurityGroupWithOrganizationSpaceAndLifecycle and all warnings", func() {
   621  					Expect(err).NotTo(HaveOccurred())
   622  					Expect(warnings).To(ConsistOf(
   623  						"warning-1", "warning-2",
   624  						"warning-3", "warning-4",
   625  						"warning-5", "warning-6",
   626  						"warning-7", "warning-8",
   627  						"warning-9", "warning-10",
   628  						"warning-11", "warning-12",
   629  						"warning-13", "warning-14",
   630  						"warning-15", "warning-16",
   631  						"warning-17", "warning-18",
   632  						"warning-19", "warning-20",
   633  						"warning-21", "warning-22",
   634  						"warning-25", "warning-26",
   635  						"warning-31", "warning-32",
   636  						"warning-33", "warning-34",
   637  						"warning-35", "warning-36",
   638  					))
   639  					expected := []SecurityGroupWithOrganizationSpaceAndLifecycle{
   640  						{
   641  							SecurityGroup: &expectedSecurityGroup1,
   642  							Organization:  &expectedOrgAll,
   643  							Space:         &expectedSpaceAll,
   644  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   645  						},
   646  						{
   647  							SecurityGroup: &expectedSecurityGroup1,
   648  							Organization:  &expectedOrg11,
   649  							Space:         &expectedSpace11,
   650  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   651  						},
   652  						{
   653  							SecurityGroup: &expectedSecurityGroup1,
   654  							Organization:  &expectedOrg11,
   655  							Space:         &expectedSpace11,
   656  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   657  						},
   658  						{
   659  							SecurityGroup: &expectedSecurityGroup1,
   660  							Organization:  &expectedOrg12,
   661  							Space:         &expectedSpace12,
   662  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   663  						},
   664  						{
   665  							SecurityGroup: &expectedSecurityGroup1,
   666  							Organization:  &expectedOrg12,
   667  							Space:         &expectedSpace12,
   668  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   669  						},
   670  						{
   671  							SecurityGroup: &expectedSecurityGroup1,
   672  							Organization:  &expectedOrg13,
   673  							Space:         &expectedSpace13,
   674  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   675  						},
   676  						{
   677  							SecurityGroup: &expectedSecurityGroup1,
   678  							Organization:  &expectedOrg13,
   679  							Space:         &expectedSpace13,
   680  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   681  						},
   682  						{
   683  							SecurityGroup: &expectedSecurityGroup2,
   684  							Organization:  &expectedOrgAll,
   685  							Space:         &expectedSpaceAll,
   686  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   687  						},
   688  						{
   689  							SecurityGroup: &expectedSecurityGroup2,
   690  							Organization:  &expectedOrg11,
   691  							Space:         &expectedSpace22,
   692  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   693  						},
   694  						{
   695  							SecurityGroup: &expectedSecurityGroup2,
   696  							Organization:  &expectedOrg21,
   697  							Space:         &expectedSpace21,
   698  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   699  						},
   700  						{
   701  							SecurityGroup: &expectedSecurityGroup2,
   702  							Organization:  &expectedOrg23,
   703  							Space:         &expectedSpace23,
   704  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   705  						},
   706  						{
   707  							SecurityGroup: &expectedSecurityGroup3,
   708  							Organization:  &Organization{},
   709  							Space:         &Space{},
   710  						},
   711  						{
   712  							SecurityGroup: &expectedSecurityGroup4,
   713  							Organization:  &expectedOrg11,
   714  							Space:         &expectedSpace32,
   715  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   716  						},
   717  						{
   718  							SecurityGroup: &expectedSecurityGroup4,
   719  							Organization:  &expectedOrg23,
   720  							Space:         &expectedSpace31,
   721  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   722  						},
   723  						{
   724  							SecurityGroup: &expectedSecurityGroup4,
   725  							Organization:  &expectedOrg33,
   726  							Space:         &expectedSpace33,
   727  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   728  						},
   729  						{
   730  							SecurityGroup: &expectedSecurityGroup5,
   731  							Organization:  &expectedOrgAll,
   732  							Space:         &expectedSpaceAll,
   733  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   734  						},
   735  						{
   736  							SecurityGroup: &expectedSecurityGroup6,
   737  							Organization:  &expectedOrgAll,
   738  							Space:         &expectedSpaceAll,
   739  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   740  						},
   741  						{
   742  							SecurityGroup: &expectedSecurityGroup7,
   743  							Organization:  &expectedOrgAll,
   744  							Space:         &expectedSpaceAll,
   745  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   746  						},
   747  						{
   748  							SecurityGroup: &expectedSecurityGroup7,
   749  							Organization:  &expectedOrgAll,
   750  							Space:         &expectedSpaceAll,
   751  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   752  						},
   753  					}
   754  					Expect(secGroupOrgSpaces).To(Equal(expected))
   755  					Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
   756  					Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(BeNil())
   757  
   758  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesCallCount()).To(Equal(7))
   759  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
   760  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(1)).To(Equal("security-group-guid-2"))
   761  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(2)).To(Equal("security-group-guid-3"))
   762  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(3)).To(Equal("security-group-guid-4"))
   763  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(4)).To(Equal("security-group-guid-5"))
   764  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(5)).To(Equal("security-group-guid-6"))
   765  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(6)).To(Equal("security-group-guid-7"))
   766  
   767  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesCallCount()).To(Equal(7))
   768  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
   769  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(1)).To(Equal("security-group-guid-2"))
   770  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(2)).To(Equal("security-group-guid-3"))
   771  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(3)).To(Equal("security-group-guid-4"))
   772  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(4)).To(Equal("security-group-guid-5"))
   773  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(5)).To(Equal("security-group-guid-6"))
   774  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(6)).To(Equal("security-group-guid-7"))
   775  
   776  					Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(6))
   777  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(0)).To(Equal("org-guid-13"))
   778  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(1)).To(Equal("org-guid-12"))
   779  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(2)).To(Equal("<<org-guid-11"))
   780  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(3)).To(Equal("org-guid-21"))
   781  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(4)).To(Equal("org-guid-23"))
   782  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(5)).To(Equal("org-guid-33"))
   783  				})
   784  			})
   785  
   786  			When("security groups bound to spaces in the staging lifecycle phase are not included", func() {
   787  				BeforeEach(func() {
   788  					includeStaging = false
   789  				})
   790  
   791  				It("returns a slice of SecurityGroupWithOrganizationSpaceAndLifecycle, excluding any which are bound only in the staging lifecycle phase, and all warnings", func() {
   792  					Expect(err).NotTo(HaveOccurred())
   793  					Expect(warnings).To(ConsistOf(
   794  						"warning-1", "warning-2",
   795  						"warning-3", "warning-4",
   796  						"warning-7", "warning-8",
   797  						"warning-9", "warning-10",
   798  						"warning-11", "warning-12",
   799  						"warning-13", "warning-14",
   800  						"warning-15", "warning-16",
   801  						"warning-17", "warning-18",
   802  						"warning-19", "warning-20",
   803  						"warning-21", "warning-22",
   804  						"warning-25", "warning-26",
   805  						"warning-31", "warning-32",
   806  						"warning-33", "warning-34",
   807  						"warning-35", "warning-36",
   808  					))
   809  
   810  					expected := []SecurityGroupWithOrganizationSpaceAndLifecycle{
   811  						{
   812  							SecurityGroup: &expectedSecurityGroup1,
   813  							Organization:  &expectedOrgAll,
   814  							Space:         &expectedSpaceAll,
   815  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   816  						},
   817  						{
   818  							SecurityGroup: &expectedSecurityGroup1,
   819  							Organization:  &expectedOrg11,
   820  							Space:         &expectedSpace11,
   821  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   822  						},
   823  						{
   824  							SecurityGroup: &expectedSecurityGroup1,
   825  							Organization:  &expectedOrg12,
   826  							Space:         &expectedSpace12,
   827  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   828  						},
   829  						{
   830  							SecurityGroup: &expectedSecurityGroup1,
   831  							Organization:  &expectedOrg13,
   832  							Space:         &expectedSpace13,
   833  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   834  						},
   835  						{
   836  							SecurityGroup: &expectedSecurityGroup2,
   837  							Organization:  &expectedOrgAll,
   838  							Space:         &expectedSpaceAll,
   839  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   840  						},
   841  						{
   842  							SecurityGroup: &expectedSecurityGroup2,
   843  							Organization:  &expectedOrg11,
   844  							Space:         &expectedSpace22,
   845  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   846  						},
   847  						{
   848  							SecurityGroup: &expectedSecurityGroup2,
   849  							Organization:  &expectedOrg21,
   850  							Space:         &expectedSpace21,
   851  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   852  						},
   853  						{
   854  							SecurityGroup: &expectedSecurityGroup2,
   855  							Organization:  &expectedOrg23,
   856  							Space:         &expectedSpace23,
   857  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   858  						},
   859  						{
   860  							SecurityGroup: &expectedSecurityGroup3,
   861  							Organization:  &Organization{},
   862  							Space:         &Space{},
   863  						},
   864  						{
   865  							SecurityGroup: &expectedSecurityGroup4,
   866  							Organization:  &expectedOrg11,
   867  							Space:         &expectedSpace32,
   868  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   869  						},
   870  						{
   871  							SecurityGroup: &expectedSecurityGroup4,
   872  							Organization:  &expectedOrg23,
   873  							Space:         &expectedSpace31,
   874  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   875  						},
   876  						{
   877  							SecurityGroup: &expectedSecurityGroup4,
   878  							Organization:  &expectedOrg33,
   879  							Space:         &expectedSpace33,
   880  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   881  						},
   882  						{
   883  							SecurityGroup: &expectedSecurityGroup5,
   884  							Organization:  &expectedOrgAll,
   885  							Space:         &expectedSpaceAll,
   886  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   887  						},
   888  						{
   889  							SecurityGroup: &expectedSecurityGroup6,
   890  							Organization:  &expectedOrgAll,
   891  							Space:         &expectedSpaceAll,
   892  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   893  						},
   894  						{
   895  							SecurityGroup: &expectedSecurityGroup7,
   896  							Organization:  &expectedOrgAll,
   897  							Space:         &expectedSpaceAll,
   898  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   899  						},
   900  						{
   901  							SecurityGroup: &expectedSecurityGroup7,
   902  							Organization:  &expectedOrgAll,
   903  							Space:         &expectedSpaceAll,
   904  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   905  						},
   906  					}
   907  					Expect(secGroupOrgSpaces).To(Equal(expected))
   908  					Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
   909  					Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(BeNil())
   910  
   911  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesCallCount()).To(Equal(7))
   912  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
   913  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(1)).To(Equal("security-group-guid-2"))
   914  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(2)).To(Equal("security-group-guid-3"))
   915  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(3)).To(Equal("security-group-guid-4"))
   916  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(4)).To(Equal("security-group-guid-5"))
   917  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(5)).To(Equal("security-group-guid-6"))
   918  					Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(6)).To(Equal("security-group-guid-7"))
   919  
   920  					Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesCallCount()).To(Equal(0))
   921  
   922  					Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(6))
   923  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(0)).To(Equal("org-guid-13"))
   924  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(1)).To(Equal("org-guid-12"))
   925  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(2)).To(Equal("<<org-guid-11"))
   926  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(3)).To(Equal("org-guid-21"))
   927  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(4)).To(Equal("org-guid-23"))
   928  					Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(5)).To(Equal("org-guid-33"))
   929  				})
   930  			})
   931  		})
   932  
   933  		When("interleaved ResourceNotFoundErrors are encountered", func() {
   934  			var (
   935  				expectedSecurityGroup1 SecurityGroup
   936  				expectedSecurityGroup2 SecurityGroup
   937  				expectedSecurityGroup3 SecurityGroup
   938  
   939  				expectedOrg13 Organization
   940  
   941  				expectedSpace11 Space
   942  				expectedSpace12 Space
   943  			)
   944  
   945  			BeforeEach(func() {
   946  				includeStaging = true
   947  
   948  				expectedSecurityGroup1 = SecurityGroup{
   949  					GUID:           "security-group-guid-1",
   950  					Name:           "security-group-1",
   951  					RunningDefault: true,
   952  				}
   953  				expectedSecurityGroup2 = SecurityGroup{
   954  					GUID:           "security-group-guid-2",
   955  					Name:           "security-group-2",
   956  					StagingDefault: true,
   957  				}
   958  				expectedSecurityGroup3 = SecurityGroup{
   959  					GUID: "security-group-guid-3",
   960  					Name: "security-group-3",
   961  				}
   962  
   963  				expectedOrg13 = Organization{
   964  					GUID: "org-guid-13",
   965  					Name: "org-13",
   966  				}
   967  
   968  				expectedSpace11 = Space{
   969  					GUID: "space-guid-11",
   970  					Name: "space-11",
   971  				}
   972  				expectedSpace12 = Space{
   973  					GUID: "space-guid-12",
   974  					Name: "space-12",
   975  				}
   976  
   977  				fakeCloudControllerClient.GetSecurityGroupsReturns(
   978  					[]ccv2.SecurityGroup{
   979  						ccv2.SecurityGroup(expectedSecurityGroup1),
   980  						ccv2.SecurityGroup(expectedSecurityGroup2),
   981  						ccv2.SecurityGroup(expectedSecurityGroup3),
   982  					},
   983  					ccv2.Warnings{"warning-1", "warning-2"},
   984  					nil,
   985  				)
   986  
   987  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(0,
   988  					nil,
   989  					ccv2.Warnings{"warning-3", "warning-4"},
   990  					ccerror.ResourceNotFoundError{Message: "running-error-1"},
   991  				)
   992  
   993  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(1,
   994  					[]ccv2.Space{
   995  						{
   996  							GUID:             "space-guid-13",
   997  							Name:             "space-13",
   998  							OrganizationGUID: "org-guid-13",
   999  						},
  1000  						{
  1001  							GUID:             "space-guid-12",
  1002  							Name:             "space-12",
  1003  							OrganizationGUID: "org-guid-12",
  1004  						},
  1005  						{
  1006  							GUID:             "space-guid-11",
  1007  							Name:             "space-11",
  1008  							OrganizationGUID: "<<org-guid-11",
  1009  						},
  1010  					},
  1011  					ccv2.Warnings{"warning-5", "warning-6"},
  1012  					nil,
  1013  				)
  1014  
  1015  				fakeCloudControllerClient.GetSecurityGroupStagingSpacesReturnsOnCall(0,
  1016  					[]ccv2.Space{},
  1017  					ccv2.Warnings{"warning-7", "warning-8"},
  1018  					ccerror.ResourceNotFoundError{Message: "staging-error-1"},
  1019  				)
  1020  
  1021  				fakeCloudControllerClient.GetSecurityGroupSpacesReturnsOnCall(2,
  1022  					[]ccv2.Space{
  1023  						{
  1024  							GUID:             "space-guid-11",
  1025  							Name:             "space-11",
  1026  							OrganizationGUID: "org-guid-13",
  1027  						},
  1028  					},
  1029  					ccv2.Warnings{"warning-9", "warning-10"},
  1030  					nil,
  1031  				)
  1032  
  1033  				fakeCloudControllerClient.GetSecurityGroupStagingSpacesReturnsOnCall(1,
  1034  					[]ccv2.Space{
  1035  						{
  1036  							GUID:             "space-guid-12",
  1037  							Name:             "space-12",
  1038  							OrganizationGUID: "org-guid-13",
  1039  						},
  1040  					},
  1041  					ccv2.Warnings{"warning-11", "warning-12"},
  1042  					nil,
  1043  				)
  1044  
  1045  				fakeCloudControllerClient.GetOrganizationReturnsOnCall(0,
  1046  					ccv2.Organization(expectedOrg13),
  1047  					ccv2.Warnings{"warning-13", "warning-14"},
  1048  					nil,
  1049  				)
  1050  			})
  1051  
  1052  			It("returns a slice of SecurityGroupWithOrganizationSpaceAndLifecycle, which have not errored-out, and all warnings", func() {
  1053  				Expect(err).NotTo(HaveOccurred())
  1054  				// Used BeEquivalentTo instead of ConsistOf to enforce order.
  1055  				Expect(warnings).To(BeEquivalentTo([]string{
  1056  					"warning-1", "warning-2",
  1057  					"warning-3", "warning-4",
  1058  					"running-error-1",
  1059  					"warning-5", "warning-6",
  1060  					"warning-7", "warning-8",
  1061  					"staging-error-1",
  1062  					"warning-9", "warning-10",
  1063  					"warning-11", "warning-12",
  1064  					"warning-13", "warning-14",
  1065  				}))
  1066  
  1067  				expected := []SecurityGroupWithOrganizationSpaceAndLifecycle{
  1068  					{
  1069  						SecurityGroup: &expectedSecurityGroup3,
  1070  						Organization:  &expectedOrg13,
  1071  						Space:         &expectedSpace11,
  1072  						Lifecycle:     constant.SecurityGroupLifecycleRunning,
  1073  					},
  1074  					{
  1075  						SecurityGroup: &expectedSecurityGroup3,
  1076  						Organization:  &expectedOrg13,
  1077  						Space:         &expectedSpace12,
  1078  						Lifecycle:     constant.SecurityGroupLifecycleStaging,
  1079  					},
  1080  				}
  1081  				Expect(secGroupOrgSpaces).To(Equal(expected))
  1082  				Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
  1083  				Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(BeNil())
  1084  
  1085  				Expect(fakeCloudControllerClient.GetSecurityGroupSpacesCallCount()).To(Equal(3))
  1086  				Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(0)).To(Equal("security-group-guid-1"))
  1087  				Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(1)).To(Equal("security-group-guid-2"))
  1088  				Expect(fakeCloudControllerClient.GetSecurityGroupSpacesArgsForCall(2)).To(Equal("security-group-guid-3"))
  1089  
  1090  				Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesCallCount()).To(Equal(2))
  1091  				Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(0)).To(Equal("security-group-guid-2"))
  1092  				Expect(fakeCloudControllerClient.GetSecurityGroupStagingSpacesArgsForCall(1)).To(Equal("security-group-guid-3"))
  1093  
  1094  				Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(1))
  1095  				Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(0)).To(Equal("org-guid-13"))
  1096  			})
  1097  		})
  1098  	})
  1099  
  1100  	Describe("GetSecurityGroupByName", func() {
  1101  		var (
  1102  			securityGroup SecurityGroup
  1103  			warnings      Warnings
  1104  			err           error
  1105  		)
  1106  
  1107  		JustBeforeEach(func() {
  1108  			securityGroup, warnings, err = actor.GetSecurityGroupByName("some-security-group")
  1109  		})
  1110  
  1111  		When("the security group exists", func() {
  1112  			BeforeEach(func() {
  1113  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1114  					[]ccv2.SecurityGroup{
  1115  						{
  1116  							GUID: "some-security-group-guid",
  1117  							Name: "some-security-group",
  1118  						},
  1119  					},
  1120  					ccv2.Warnings{"warning-1", "warning-2"},
  1121  					nil,
  1122  				)
  1123  			})
  1124  
  1125  			It("returns the security group and all warnings", func() {
  1126  				Expect(err).ToNot(HaveOccurred())
  1127  				Expect(securityGroup.GUID).To(Equal("some-security-group-guid"))
  1128  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
  1129  
  1130  				Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
  1131  				filters := fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)
  1132  				Expect(filters).To(Equal(
  1133  					[]ccv2.Filter{{
  1134  						Type:     constant.NameFilter,
  1135  						Operator: constant.EqualOperator,
  1136  						Values:   []string{"some-security-group"},
  1137  					}}))
  1138  			})
  1139  		})
  1140  
  1141  		When("the security group does not exist", func() {
  1142  			BeforeEach(func() {
  1143  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1144  					[]ccv2.SecurityGroup{},
  1145  					ccv2.Warnings{"warning-1", "warning-2"},
  1146  					nil,
  1147  				)
  1148  			})
  1149  
  1150  			It("returns a SecurityGroupNotFound error", func() {
  1151  				Expect(err).To(MatchError(actionerror.SecurityGroupNotFoundError{Name: "some-security-group"}))
  1152  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
  1153  			})
  1154  		})
  1155  
  1156  		Context("an error occurs", func() {
  1157  			var returnedError error
  1158  
  1159  			BeforeEach(func() {
  1160  				returnedError = errors.New("get-security-groups-error")
  1161  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1162  					[]ccv2.SecurityGroup{},
  1163  					ccv2.Warnings{"warning-1", "warning-2"},
  1164  					returnedError,
  1165  				)
  1166  			})
  1167  
  1168  			It("returns the error and all warnings", func() {
  1169  				Expect(err).To(MatchError(returnedError))
  1170  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
  1171  			})
  1172  		})
  1173  	})
  1174  
  1175  	Describe("BindSecurityGroupToSpace", func() {
  1176  		var (
  1177  			lifecycle constant.SecurityGroupLifecycle
  1178  			err       error
  1179  			warnings  []string
  1180  		)
  1181  
  1182  		JustBeforeEach(func() {
  1183  			warnings, err = actor.BindSecurityGroupToSpace("some-security-group-guid", "some-space-guid", lifecycle)
  1184  		})
  1185  
  1186  		When("the lifecycle is neither running nor staging", func() {
  1187  			BeforeEach(func() {
  1188  				lifecycle = "bill & ted"
  1189  			})
  1190  
  1191  			It("returns and appropriate error", func() {
  1192  				Expect(err).To(MatchError(fmt.Sprintf("Invalid lifecycle: %s", lifecycle)))
  1193  			})
  1194  		})
  1195  
  1196  		When("the lifecycle is running", func() {
  1197  			BeforeEach(func() {
  1198  				lifecycle = constant.SecurityGroupLifecycleRunning
  1199  			})
  1200  
  1201  			When("binding the space does not return an error", func() {
  1202  				BeforeEach(func() {
  1203  					fakeCloudControllerClient.UpdateSecurityGroupSpaceReturns(
  1204  						ccv2.Warnings{"warning-1", "warning-2"},
  1205  						nil,
  1206  					)
  1207  				})
  1208  
  1209  				It("returns warnings and no error", func() {
  1210  					Expect(err).ToNot(HaveOccurred())
  1211  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
  1212  					Expect(fakeCloudControllerClient.UpdateSecurityGroupSpaceCallCount()).To(Equal(1))
  1213  					securityGroupGUID, spaceGUID := fakeCloudControllerClient.UpdateSecurityGroupSpaceArgsForCall(0)
  1214  					Expect(securityGroupGUID).To(Equal("some-security-group-guid"))
  1215  					Expect(spaceGUID).To(Equal("some-space-guid"))
  1216  				})
  1217  			})
  1218  
  1219  			When("binding the space returns an error", func() {
  1220  				var returnedError error
  1221  				BeforeEach(func() {
  1222  					returnedError = errors.New("associate-space-error")
  1223  					fakeCloudControllerClient.UpdateSecurityGroupSpaceReturns(
  1224  						ccv2.Warnings{"warning-1", "warning-2"},
  1225  						returnedError,
  1226  					)
  1227  				})
  1228  
  1229  				It("returns the error and warnings", func() {
  1230  					Expect(err).To(Equal(returnedError))
  1231  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
  1232  				})
  1233  			})
  1234  		})
  1235  
  1236  		When("the lifecycle is staging", func() {
  1237  			BeforeEach(func() {
  1238  				lifecycle = constant.SecurityGroupLifecycleStaging
  1239  			})
  1240  
  1241  			When("binding the space does not return an error", func() {
  1242  				BeforeEach(func() {
  1243  					fakeCloudControllerClient.UpdateSecurityGroupStagingSpaceReturns(
  1244  						ccv2.Warnings{"warning-1", "warning-2"},
  1245  						nil,
  1246  					)
  1247  				})
  1248  
  1249  				It("returns warnings and no error", func() {
  1250  					Expect(err).ToNot(HaveOccurred())
  1251  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
  1252  					Expect(fakeCloudControllerClient.UpdateSecurityGroupStagingSpaceCallCount()).To(Equal(1))
  1253  					securityGroupGUID, spaceGUID := fakeCloudControllerClient.UpdateSecurityGroupStagingSpaceArgsForCall(0)
  1254  					Expect(securityGroupGUID).To(Equal("some-security-group-guid"))
  1255  					Expect(spaceGUID).To(Equal("some-space-guid"))
  1256  				})
  1257  			})
  1258  
  1259  			When("binding the space returns an error", func() {
  1260  				var returnedError error
  1261  				BeforeEach(func() {
  1262  					returnedError = errors.New("associate-space-error")
  1263  					fakeCloudControllerClient.UpdateSecurityGroupStagingSpaceReturns(
  1264  						ccv2.Warnings{"warning-1", "warning-2"},
  1265  						returnedError,
  1266  					)
  1267  				})
  1268  
  1269  				It("returns the error and warnings", func() {
  1270  					Expect(err).To(Equal(returnedError))
  1271  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
  1272  				})
  1273  			})
  1274  		})
  1275  	})
  1276  
  1277  	Describe("GetSpaceRunningSecurityGroupsBySpace", func() {
  1278  		When("the space exists and there are no errors", func() {
  1279  			BeforeEach(func() {
  1280  				fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1281  					[]ccv2.SecurityGroup{
  1282  						{
  1283  							Name: "some-shared-security-group",
  1284  						},
  1285  						{
  1286  							Name: "some-running-security-group",
  1287  						},
  1288  					},
  1289  					ccv2.Warnings{"warning-1", "warning-2"},
  1290  					nil,
  1291  				)
  1292  			})
  1293  
  1294  			It("returns the security groups and warnings", func() {
  1295  				securityGroups, warnings, err := actor.GetSpaceRunningSecurityGroupsBySpace("space-guid")
  1296  				Expect(err).NotTo(HaveOccurred())
  1297  				Expect(warnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
  1298  				Expect(securityGroups).To(Equal(
  1299  					[]SecurityGroup{
  1300  						{
  1301  							Name: "some-shared-security-group",
  1302  						},
  1303  						{
  1304  							Name: "some-running-security-group",
  1305  						},
  1306  					}))
  1307  
  1308  				Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  1309  				spaceGUID, queries := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  1310  				Expect(spaceGUID).To(Equal("space-guid"))
  1311  				Expect(queries).To(BeNil())
  1312  			})
  1313  		})
  1314  
  1315  		When("the space does not exist", func() {
  1316  			BeforeEach(func() {
  1317  				fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1318  					nil,
  1319  					nil,
  1320  					ccerror.ResourceNotFoundError{})
  1321  			})
  1322  
  1323  			It("returns an SpaceNotFoundError", func() {
  1324  				_, _, err := actor.GetSpaceRunningSecurityGroupsBySpace("space-guid")
  1325  				Expect(err).To(MatchError(actionerror.SpaceNotFoundError{GUID: "space-guid"}))
  1326  			})
  1327  		})
  1328  
  1329  		When("there is an error", func() {
  1330  			var expectedErr error
  1331  
  1332  			BeforeEach(func() {
  1333  				expectedErr = errors.New("banana")
  1334  				fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1335  					nil,
  1336  					ccv2.Warnings{"warning-1", "warning-2"},
  1337  					expectedErr)
  1338  			})
  1339  
  1340  			It("returns the error and warnings", func() {
  1341  				_, warnings, err := actor.GetSpaceRunningSecurityGroupsBySpace("space-guid")
  1342  				Expect(warnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
  1343  				Expect(err).To(MatchError(expectedErr))
  1344  			})
  1345  		})
  1346  	})
  1347  
  1348  	Describe("GetSpaceStagingSecurityGroupsBySpace", func() {
  1349  		When("the space exists and there are no errors", func() {
  1350  			BeforeEach(func() {
  1351  				fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1352  					[]ccv2.SecurityGroup{
  1353  						{
  1354  							Name: "some-shared-security-group",
  1355  						},
  1356  						{
  1357  							Name: "some-staging-security-group",
  1358  						},
  1359  					},
  1360  					ccv2.Warnings{"warning-1", "warning-2"},
  1361  					nil,
  1362  				)
  1363  			})
  1364  
  1365  			It("returns the security groups and warnings", func() {
  1366  				securityGroups, warnings, err := actor.GetSpaceStagingSecurityGroupsBySpace("space-guid")
  1367  				Expect(err).NotTo(HaveOccurred())
  1368  				Expect(warnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
  1369  				Expect(securityGroups).To(Equal(
  1370  					[]SecurityGroup{
  1371  						{
  1372  							Name: "some-shared-security-group",
  1373  						},
  1374  						{
  1375  							Name: "some-staging-security-group",
  1376  						},
  1377  					}))
  1378  
  1379  				Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  1380  				spaceGUID, queries := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  1381  				Expect(spaceGUID).To(Equal("space-guid"))
  1382  				Expect(queries).To(BeNil())
  1383  			})
  1384  		})
  1385  
  1386  		When("the space does not exist", func() {
  1387  			BeforeEach(func() {
  1388  				fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1389  					nil,
  1390  					nil,
  1391  					ccerror.ResourceNotFoundError{})
  1392  			})
  1393  
  1394  			It("returns an SpaceNotFoundError", func() {
  1395  				_, _, err := actor.GetSpaceStagingSecurityGroupsBySpace("space-guid")
  1396  				Expect(err).To(MatchError(actionerror.SpaceNotFoundError{GUID: "space-guid"}))
  1397  			})
  1398  		})
  1399  
  1400  		When("there is an error", func() {
  1401  			var expectedErr error
  1402  
  1403  			BeforeEach(func() {
  1404  				expectedErr = errors.New("banana")
  1405  				fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1406  					nil,
  1407  					ccv2.Warnings{"warning-1", "warning-2"},
  1408  					expectedErr)
  1409  			})
  1410  
  1411  			It("returns the error and warnings", func() {
  1412  				_, warnings, err := actor.GetSpaceStagingSecurityGroupsBySpace("space-guid")
  1413  				Expect(warnings).To(ConsistOf([]string{"warning-1", "warning-2"}))
  1414  				Expect(err).To(MatchError(expectedErr))
  1415  			})
  1416  		})
  1417  	})
  1418  
  1419  	Describe("UnbindSecurityGroupByNameAndSpace", func() {
  1420  		var (
  1421  			lifecycle constant.SecurityGroupLifecycle
  1422  			warnings  Warnings
  1423  			err       error
  1424  		)
  1425  
  1426  		JustBeforeEach(func() {
  1427  			warnings, err = actor.UnbindSecurityGroupByNameAndSpace("some-security-group", "some-space-guid", lifecycle)
  1428  		})
  1429  
  1430  		When("the requested lifecycle is neither running nor staging", func() {
  1431  			BeforeEach(func() {
  1432  				lifecycle = "bill & ted"
  1433  			})
  1434  
  1435  			It("returns and appropriate error", func() {
  1436  				Expect(err).To(MatchError(fmt.Sprintf("Invalid lifecycle: %s", lifecycle)))
  1437  			})
  1438  		})
  1439  
  1440  		When("the security group is not found", func() {
  1441  			BeforeEach(func() {
  1442  				lifecycle = constant.SecurityGroupLifecycleStaging
  1443  
  1444  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1445  					[]ccv2.SecurityGroup{},
  1446  					ccv2.Warnings{"security-group-warning"},
  1447  					nil)
  1448  			})
  1449  
  1450  			It("returns the error and all warnings", func() {
  1451  				Expect(warnings).To(ConsistOf([]string{"security-group-warning"}))
  1452  				Expect(err).To(MatchError(actionerror.SecurityGroupNotFoundError{Name: "some-security-group"}))
  1453  			})
  1454  		})
  1455  
  1456  		When("an error is encountered fetching security groups", func() {
  1457  			var returnedError error
  1458  
  1459  			BeforeEach(func() {
  1460  				lifecycle = constant.SecurityGroupLifecycleRunning
  1461  
  1462  				returnedError = errors.New("get-security-groups-error")
  1463  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1464  					[]ccv2.SecurityGroup{},
  1465  					ccv2.Warnings{"warning-1", "warning-2"},
  1466  					returnedError)
  1467  			})
  1468  
  1469  			It("returns all warnings", func() {
  1470  				Expect(err).To(MatchError(returnedError))
  1471  				Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"}))
  1472  			})
  1473  		})
  1474  
  1475  		When("the requested lifecycle is running", func() {
  1476  			BeforeEach(func() {
  1477  				lifecycle = constant.SecurityGroupLifecycleRunning
  1478  
  1479  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1480  					[]ccv2.SecurityGroup{{
  1481  						Name: "some-security-group",
  1482  						GUID: "some-security-group-guid",
  1483  					}},
  1484  					ccv2.Warnings{"warning-1", "warning-2"},
  1485  					nil)
  1486  			})
  1487  
  1488  			When("the security group is bound to running", func() {
  1489  				BeforeEach(func() {
  1490  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1491  						[]ccv2.SecurityGroup{
  1492  							{
  1493  								Name: "some-security-group",
  1494  								GUID: "some-security-group-guid",
  1495  							},
  1496  						},
  1497  						ccv2.Warnings{"warning-3", "warning-4"},
  1498  						nil,
  1499  					)
  1500  				})
  1501  
  1502  				When("an error is encountered checking whether the security group is bound to the space in the running phase", func() {
  1503  					var returnedError error
  1504  
  1505  					BeforeEach(func() {
  1506  						returnedError = errors.New("get-security-groups-error")
  1507  						fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1508  							[]ccv2.SecurityGroup{
  1509  								{
  1510  									Name: "some-security-group",
  1511  									GUID: "some-security-group-guid",
  1512  								},
  1513  							},
  1514  							ccv2.Warnings{"warning-3", "warning-4"},
  1515  							returnedError,
  1516  						)
  1517  					})
  1518  
  1519  					It("returns all warnings", func() {
  1520  						Expect(err).To(MatchError(returnedError))
  1521  						Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2", "warning-3", "warning-4"}))
  1522  					})
  1523  				})
  1524  
  1525  				When("an error is encountered unbinding the security group from the space", func() {
  1526  					var returnedError error
  1527  
  1528  					BeforeEach(func() {
  1529  						returnedError = errors.New("associate-space-error")
  1530  						fakeCloudControllerClient.DeleteSecurityGroupSpaceReturns(
  1531  							ccv2.Warnings{"warning-5", "warning-6"},
  1532  							returnedError)
  1533  					})
  1534  
  1535  					It("returns the error and all warnings", func() {
  1536  						Expect(warnings).To(ConsistOf([]string{
  1537  							"warning-1",
  1538  							"warning-2",
  1539  							"warning-3",
  1540  							"warning-4",
  1541  							"warning-5",
  1542  							"warning-6",
  1543  						}))
  1544  						Expect(err).To(MatchError(returnedError))
  1545  					})
  1546  				})
  1547  
  1548  				When("no errors are encountered", func() {
  1549  					BeforeEach(func() {
  1550  						fakeCloudControllerClient.DeleteSecurityGroupSpaceReturns(
  1551  							ccv2.Warnings{"warning-5", "warning-6"},
  1552  							nil)
  1553  					})
  1554  
  1555  					It("returns all warnings", func() {
  1556  						Expect(warnings).To(ConsistOf([]string{
  1557  							"warning-1",
  1558  							"warning-2",
  1559  							"warning-3",
  1560  							"warning-4",
  1561  							"warning-5",
  1562  							"warning-6",
  1563  						}))
  1564  						Expect(err).ToNot(HaveOccurred())
  1565  
  1566  						Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
  1567  						Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(Equal([]ccv2.Filter{{
  1568  							Type:     constant.NameFilter,
  1569  							Operator: constant.EqualOperator,
  1570  							Values:   []string{"some-security-group"},
  1571  						}}))
  1572  
  1573  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  1574  						spaceGUID, queries := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  1575  						Expect(spaceGUID).To(Equal("some-space-guid"))
  1576  						Expect(queries).To(Equal([]ccv2.Filter{{
  1577  							Type:     constant.NameFilter,
  1578  							Operator: constant.EqualOperator,
  1579  							Values:   []string{"some-security-group"},
  1580  						}}))
  1581  
  1582  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(0))
  1583  
  1584  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(1))
  1585  						securityGroupGUID, spaceGUID := fakeCloudControllerClient.DeleteSecurityGroupSpaceArgsForCall(0)
  1586  						Expect(securityGroupGUID).To(Equal("some-security-group-guid"))
  1587  						Expect(spaceGUID).To(Equal("some-space-guid"))
  1588  
  1589  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  1590  					})
  1591  				})
  1592  			})
  1593  
  1594  			When("the security group is bound to neither running nor staging", func() {
  1595  				BeforeEach(func() {
  1596  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1597  						[]ccv2.SecurityGroup{},
  1598  						ccv2.Warnings{"warning-3", "warning-4"},
  1599  						nil,
  1600  					)
  1601  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1602  						[]ccv2.SecurityGroup{},
  1603  						ccv2.Warnings{"warning-5", "warning-6"},
  1604  						nil,
  1605  					)
  1606  				})
  1607  
  1608  				When("no errors are encountered", func() {
  1609  					It("returns all warnings", func() {
  1610  						Expect(warnings).To(ConsistOf([]string{
  1611  							"warning-1",
  1612  							"warning-2",
  1613  							"warning-3",
  1614  							"warning-4",
  1615  							"warning-5",
  1616  							"warning-6",
  1617  						}))
  1618  						Expect(err).ToNot(HaveOccurred())
  1619  
  1620  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  1621  						spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  1622  						Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  1623  						Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  1624  							Type:     constant.NameFilter,
  1625  							Operator: constant.EqualOperator,
  1626  							Values:   []string{"some-security-group"},
  1627  						}}))
  1628  
  1629  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  1630  						spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  1631  						Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  1632  						Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  1633  							Type:     constant.NameFilter,
  1634  							Operator: constant.EqualOperator,
  1635  							Values:   []string{"some-security-group"},
  1636  						}}))
  1637  
  1638  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  1639  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  1640  					})
  1641  				})
  1642  			})
  1643  
  1644  			When("the security group is bound to staging", func() {
  1645  				BeforeEach(func() {
  1646  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1647  						[]ccv2.SecurityGroup{},
  1648  						ccv2.Warnings{"warning-3", "warning-4"},
  1649  						nil,
  1650  					)
  1651  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1652  						[]ccv2.SecurityGroup{
  1653  							{
  1654  								Name: "some-security-group",
  1655  								GUID: "some-security-group-guid",
  1656  							},
  1657  						},
  1658  						ccv2.Warnings{"warning-5", "warning-6"},
  1659  						nil,
  1660  					)
  1661  				})
  1662  
  1663  				It("returns all warnings and a SecurityGroupNotBoundError", func() {
  1664  					Expect(warnings).To(ConsistOf([]string{
  1665  						"warning-1",
  1666  						"warning-2",
  1667  						"warning-3",
  1668  						"warning-4",
  1669  						"warning-5",
  1670  						"warning-6",
  1671  					}))
  1672  					Expect(err).To(MatchError(actionerror.SecurityGroupNotBoundError{
  1673  						Name:      "some-security-group",
  1674  						Lifecycle: lifecycle,
  1675  					}))
  1676  
  1677  					Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  1678  					spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  1679  					Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  1680  					Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  1681  						Type:     constant.NameFilter,
  1682  						Operator: constant.EqualOperator,
  1683  						Values:   []string{"some-security-group"},
  1684  					}}))
  1685  
  1686  					Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  1687  					spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  1688  					Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  1689  					Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  1690  						Type:     constant.NameFilter,
  1691  						Operator: constant.EqualOperator,
  1692  						Values:   []string{"some-security-group"},
  1693  					}}))
  1694  
  1695  					Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  1696  					Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  1697  				})
  1698  			})
  1699  		})
  1700  
  1701  		When("the requested lifecycle is staging", func() {
  1702  			BeforeEach(func() {
  1703  				lifecycle = constant.SecurityGroupLifecycleStaging
  1704  
  1705  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1706  					[]ccv2.SecurityGroup{{
  1707  						Name: "some-security-group",
  1708  						GUID: "some-security-group-guid",
  1709  					}},
  1710  					ccv2.Warnings{"warning-1", "warning-2"},
  1711  					nil)
  1712  			})
  1713  
  1714  			When("an error is encountered checking whether the security group is bound to the space in the staging phase", func() {
  1715  				var returnedError error
  1716  
  1717  				BeforeEach(func() {
  1718  					returnedError = errors.New("get-space-staging-security-groups-error")
  1719  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1720  						[]ccv2.SecurityGroup{},
  1721  						ccv2.Warnings{"warning-3", "warning-4"},
  1722  						returnedError,
  1723  					)
  1724  				})
  1725  
  1726  				It("returns all warnings", func() {
  1727  					Expect(err).To(MatchError(returnedError))
  1728  					Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2", "warning-3", "warning-4"}))
  1729  				})
  1730  			})
  1731  
  1732  			When("the security group is bound to staging", func() {
  1733  				BeforeEach(func() {
  1734  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1735  						[]ccv2.SecurityGroup{
  1736  							{
  1737  								Name: "some-security-group",
  1738  								GUID: "some-security-group-guid",
  1739  							},
  1740  						},
  1741  						ccv2.Warnings{"warning-3", "warning-4"},
  1742  						nil,
  1743  					)
  1744  				})
  1745  
  1746  				When("an error is encountered unbinding the security group the space", func() {
  1747  					var returnedError error
  1748  
  1749  					BeforeEach(func() {
  1750  						returnedError = errors.New("associate-space-error")
  1751  						fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceReturns(
  1752  							ccv2.Warnings{"warning-5", "warning-6"},
  1753  							returnedError)
  1754  					})
  1755  
  1756  					It("returns the error and all warnings", func() {
  1757  						Expect(err).To(MatchError(returnedError))
  1758  						Expect(warnings).To(ConsistOf([]string{
  1759  							"warning-1",
  1760  							"warning-2",
  1761  							"warning-3",
  1762  							"warning-4",
  1763  							"warning-5",
  1764  							"warning-6",
  1765  						}))
  1766  					})
  1767  				})
  1768  
  1769  				When("no errors are encountered", func() {
  1770  					BeforeEach(func() {
  1771  						fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceReturns(
  1772  							ccv2.Warnings{"warning-5", "warning-6"},
  1773  							nil)
  1774  					})
  1775  
  1776  					It("unbinds and returns all warnings", func() {
  1777  						Expect(err).ToNot(HaveOccurred())
  1778  						Expect(warnings).To(ConsistOf([]string{
  1779  							"warning-1",
  1780  							"warning-2",
  1781  							"warning-3",
  1782  							"warning-4",
  1783  							"warning-5",
  1784  							"warning-6",
  1785  						}))
  1786  
  1787  						Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
  1788  						Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(Equal([]ccv2.Filter{{
  1789  							Type:     constant.NameFilter,
  1790  							Operator: constant.EqualOperator,
  1791  							Values:   []string{"some-security-group"},
  1792  						}}))
  1793  
  1794  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  1795  						spaceGUID, queries := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  1796  						Expect(spaceGUID).To(Equal("some-space-guid"))
  1797  						Expect(queries).To(Equal([]ccv2.Filter{{
  1798  							Type:     constant.NameFilter,
  1799  							Operator: constant.EqualOperator,
  1800  							Values:   []string{"some-security-group"},
  1801  						}}))
  1802  
  1803  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(0))
  1804  
  1805  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(1))
  1806  						securityGroupGUID, spaceGUID := fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceArgsForCall(0)
  1807  						Expect(securityGroupGUID).To(Equal("some-security-group-guid"))
  1808  						Expect(spaceGUID).To(Equal("some-space-guid"))
  1809  
  1810  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  1811  					})
  1812  				})
  1813  			})
  1814  
  1815  			When("the security group is bound to neither running nor staging", func() {
  1816  				BeforeEach(func() {
  1817  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1818  						[]ccv2.SecurityGroup{},
  1819  						ccv2.Warnings{"warning-3", "warning-4"},
  1820  						nil,
  1821  					)
  1822  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1823  						[]ccv2.SecurityGroup{},
  1824  						ccv2.Warnings{"warning-5", "warning-6"},
  1825  						nil,
  1826  					)
  1827  				})
  1828  
  1829  				When("no errors are encountered", func() {
  1830  					It("returns all warnings", func() {
  1831  						Expect(err).ToNot(HaveOccurred())
  1832  						Expect(warnings).To(ConsistOf([]string{
  1833  							"warning-1",
  1834  							"warning-2",
  1835  							"warning-3",
  1836  							"warning-4",
  1837  							"warning-5",
  1838  							"warning-6",
  1839  						}))
  1840  
  1841  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  1842  						spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  1843  						Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  1844  						Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  1845  							Type:     constant.NameFilter,
  1846  							Operator: constant.EqualOperator,
  1847  							Values:   []string{"some-security-group"},
  1848  						}}))
  1849  
  1850  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  1851  						spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  1852  						Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  1853  						Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  1854  							Type:     constant.NameFilter,
  1855  							Operator: constant.EqualOperator,
  1856  							Values:   []string{"some-security-group"},
  1857  						}}))
  1858  
  1859  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  1860  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  1861  					})
  1862  				})
  1863  			})
  1864  
  1865  			When("the security group is bound to running", func() {
  1866  				BeforeEach(func() {
  1867  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1868  						[]ccv2.SecurityGroup{},
  1869  						ccv2.Warnings{"warning-3", "warning-4"},
  1870  						nil,
  1871  					)
  1872  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1873  						[]ccv2.SecurityGroup{
  1874  							{
  1875  								Name: "some-security-group",
  1876  								GUID: "some-security-group-guid",
  1877  							},
  1878  						},
  1879  						ccv2.Warnings{"warning-5", "warning-6"},
  1880  						nil,
  1881  					)
  1882  				})
  1883  
  1884  				It("returns all warnings and a SecurityGroupNotBoundError", func() {
  1885  					Expect(warnings).To(ConsistOf([]string{
  1886  						"warning-1",
  1887  						"warning-2",
  1888  						"warning-3",
  1889  						"warning-4",
  1890  						"warning-5",
  1891  						"warning-6",
  1892  					}))
  1893  
  1894  					Expect(err).To(MatchError(actionerror.SecurityGroupNotBoundError{
  1895  						Name:      "some-security-group",
  1896  						Lifecycle: lifecycle,
  1897  					}))
  1898  
  1899  					Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  1900  					spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  1901  					Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  1902  					Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  1903  						Type:     constant.NameFilter,
  1904  						Operator: constant.EqualOperator,
  1905  						Values:   []string{"some-security-group"},
  1906  					}}))
  1907  
  1908  					Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  1909  					spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  1910  					Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  1911  					Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  1912  						Type:     constant.NameFilter,
  1913  						Operator: constant.EqualOperator,
  1914  						Values:   []string{"some-security-group"},
  1915  					}}))
  1916  
  1917  					Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  1918  					Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  1919  				})
  1920  			})
  1921  
  1922  			When("it is not bound to staging and an error occurs checking whether bound to running", func() {
  1923  				var returnedError error
  1924  
  1925  				BeforeEach(func() {
  1926  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  1927  						[]ccv2.SecurityGroup{},
  1928  						ccv2.Warnings{"warning-3", "warning-4"},
  1929  						nil,
  1930  					)
  1931  					returnedError = errors.New("get-space-running-security-groups-error")
  1932  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  1933  						[]ccv2.SecurityGroup{
  1934  							{
  1935  								Name: "some-security-group",
  1936  								GUID: "some-security-group-guid",
  1937  							},
  1938  						},
  1939  						ccv2.Warnings{"warning-5", "warning-6"},
  1940  						returnedError,
  1941  					)
  1942  				})
  1943  
  1944  				It("returns all warnings", func() {
  1945  					Expect(err).To(MatchError(returnedError))
  1946  					Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6"}))
  1947  				})
  1948  			})
  1949  		})
  1950  	})
  1951  
  1952  	Describe("UnbindSecurityGroupByNameOrganizationNameAndSpaceName", func() {
  1953  		var (
  1954  			lifecycle constant.SecurityGroupLifecycle
  1955  			warnings  []string
  1956  			err       error
  1957  		)
  1958  
  1959  		JustBeforeEach(func() {
  1960  			warnings, err = actor.UnbindSecurityGroupByNameOrganizationNameAndSpaceName("some-security-group", "some-org", "some-space", lifecycle)
  1961  		})
  1962  
  1963  		When("the requested lifecycle is neither running nor staging", func() {
  1964  			BeforeEach(func() {
  1965  				lifecycle = "bill & ted"
  1966  			})
  1967  
  1968  			It("returns and appropriate error", func() {
  1969  				Expect(err).To(MatchError(fmt.Sprintf("Invalid lifecycle: %s", lifecycle)))
  1970  			})
  1971  		})
  1972  
  1973  		When("the security group is not found", func() {
  1974  			BeforeEach(func() {
  1975  				lifecycle = constant.SecurityGroupLifecycleRunning
  1976  
  1977  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1978  					[]ccv2.SecurityGroup{},
  1979  					ccv2.Warnings{"security-group-warning"},
  1980  					nil)
  1981  			})
  1982  
  1983  			It("returns the error and all warnings", func() {
  1984  				Expect(warnings).To(ConsistOf([]string{"security-group-warning"}))
  1985  				Expect(err).To(MatchError(actionerror.SecurityGroupNotFoundError{Name: "some-security-group"}))
  1986  			})
  1987  		})
  1988  
  1989  		When("an error is encountered getting the organization", func() {
  1990  			BeforeEach(func() {
  1991  				lifecycle = constant.SecurityGroupLifecycleRunning
  1992  
  1993  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  1994  					[]ccv2.SecurityGroup{{
  1995  						Name: "some-security-group",
  1996  						GUID: "some-security-group-guid",
  1997  					}},
  1998  					ccv2.Warnings{"security-group-warning"},
  1999  					nil)
  2000  				fakeCloudControllerClient.GetOrganizationsReturns(
  2001  					[]ccv2.Organization{},
  2002  					ccv2.Warnings{"org-warning"},
  2003  					nil)
  2004  			})
  2005  
  2006  			It("returns the error and all warnings", func() {
  2007  				Expect(warnings).To(ConsistOf([]string{"security-group-warning", "org-warning"}))
  2008  				Expect(err).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
  2009  			})
  2010  		})
  2011  
  2012  		When("an error is encountered getting the space", func() {
  2013  			BeforeEach(func() {
  2014  				lifecycle = constant.SecurityGroupLifecycleRunning
  2015  
  2016  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  2017  					[]ccv2.SecurityGroup{{
  2018  						Name: "some-security-group",
  2019  						GUID: "some-security-group-guid",
  2020  					}},
  2021  					ccv2.Warnings{"security-group-warning"},
  2022  					nil)
  2023  				fakeCloudControllerClient.GetOrganizationsReturns(
  2024  					[]ccv2.Organization{{
  2025  						Name: "some-org",
  2026  						GUID: "some-org-guid",
  2027  					}},
  2028  					ccv2.Warnings{"org-warning"},
  2029  					nil)
  2030  				fakeCloudControllerClient.GetSpacesReturns(
  2031  					[]ccv2.Space{},
  2032  					ccv2.Warnings{"space-warning"},
  2033  					nil)
  2034  			})
  2035  
  2036  			It("returns the error and all warnings", func() {
  2037  				Expect(warnings).To(ConsistOf([]string{"security-group-warning", "org-warning", "space-warning"}))
  2038  				Expect(err).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"}))
  2039  			})
  2040  		})
  2041  
  2042  		When("the requested lifecycle is running", func() {
  2043  			BeforeEach(func() {
  2044  				lifecycle = constant.SecurityGroupLifecycleRunning
  2045  
  2046  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  2047  					[]ccv2.SecurityGroup{{
  2048  						Name: "some-security-group",
  2049  						GUID: "some-security-group-guid",
  2050  					}},
  2051  					ccv2.Warnings{"warning-1", "warning-2"},
  2052  					nil)
  2053  				fakeCloudControllerClient.GetOrganizationsReturns(
  2054  					[]ccv2.Organization{{
  2055  						Name: "some-org",
  2056  						GUID: "some-org-guid",
  2057  					}},
  2058  					ccv2.Warnings{"warning-3", "warning-4"},
  2059  					nil)
  2060  				fakeCloudControllerClient.GetSpacesReturns(
  2061  					[]ccv2.Space{{
  2062  						Name: "some-space",
  2063  						GUID: "some-space-guid",
  2064  					}},
  2065  					ccv2.Warnings{"warning-5", "warning-6"},
  2066  					nil)
  2067  			})
  2068  
  2069  			When("the security group is bound to running", func() {
  2070  				BeforeEach(func() {
  2071  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  2072  						[]ccv2.SecurityGroup{
  2073  							{
  2074  								Name: "some-security-group",
  2075  								GUID: "some-security-group-guid",
  2076  							},
  2077  						},
  2078  						ccv2.Warnings{"warning-7", "warning-8"},
  2079  						nil,
  2080  					)
  2081  				})
  2082  
  2083  				When("an error is encountered unbinding the security group from the space", func() {
  2084  					var returnedError error
  2085  
  2086  					BeforeEach(func() {
  2087  						returnedError = errors.New("associate-space-error")
  2088  						fakeCloudControllerClient.DeleteSecurityGroupSpaceReturns(
  2089  							ccv2.Warnings{"warning-9", "warning-10"},
  2090  							returnedError)
  2091  					})
  2092  
  2093  					It("returns the error and all warnings", func() {
  2094  						Expect(warnings).To(ConsistOf([]string{
  2095  							"warning-1",
  2096  							"warning-2",
  2097  							"warning-3",
  2098  							"warning-4",
  2099  							"warning-5",
  2100  							"warning-6",
  2101  							"warning-7",
  2102  							"warning-8",
  2103  							"warning-9",
  2104  							"warning-10",
  2105  						}))
  2106  						Expect(err).To(MatchError(returnedError))
  2107  					})
  2108  				})
  2109  
  2110  				When("no errors are encountered", func() {
  2111  					BeforeEach(func() {
  2112  						fakeCloudControllerClient.DeleteSecurityGroupSpaceReturns(
  2113  							ccv2.Warnings{"warning-9", "warning-10"},
  2114  							nil)
  2115  					})
  2116  
  2117  					It("returns all warnings", func() {
  2118  						Expect(warnings).To(ConsistOf([]string{
  2119  							"warning-1",
  2120  							"warning-2",
  2121  							"warning-3",
  2122  							"warning-4",
  2123  							"warning-5",
  2124  							"warning-6",
  2125  							"warning-7",
  2126  							"warning-8",
  2127  							"warning-9",
  2128  							"warning-10",
  2129  						}))
  2130  						Expect(err).ToNot(HaveOccurred())
  2131  
  2132  						Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
  2133  						Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(Equal([]ccv2.Filter{{
  2134  							Type:     constant.NameFilter,
  2135  							Operator: constant.EqualOperator,
  2136  							Values:   []string{"some-security-group"},
  2137  						}}))
  2138  
  2139  						Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
  2140  						Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal([]ccv2.Filter{{
  2141  							Type:     constant.NameFilter,
  2142  							Operator: constant.EqualOperator,
  2143  							Values:   []string{"some-org"},
  2144  						}}))
  2145  
  2146  						Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
  2147  						Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal([]ccv2.Filter{{
  2148  							Type:     constant.NameFilter,
  2149  							Operator: constant.EqualOperator,
  2150  							Values:   []string{"some-space"},
  2151  						}, {
  2152  							Type:     constant.OrganizationGUIDFilter,
  2153  							Operator: constant.EqualOperator,
  2154  							Values:   []string{"some-org-guid"},
  2155  						}}))
  2156  
  2157  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  2158  						spaceGUID, queries := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  2159  						Expect(spaceGUID).To(Equal("some-space-guid"))
  2160  						Expect(queries).To(Equal([]ccv2.Filter{{
  2161  							Type:     constant.NameFilter,
  2162  							Operator: constant.EqualOperator,
  2163  							Values:   []string{"some-security-group"},
  2164  						}}))
  2165  
  2166  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(0))
  2167  
  2168  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(1))
  2169  						securityGroupGUID, spaceGUID := fakeCloudControllerClient.DeleteSecurityGroupSpaceArgsForCall(0)
  2170  						Expect(securityGroupGUID).To(Equal("some-security-group-guid"))
  2171  						Expect(spaceGUID).To(Equal("some-space-guid"))
  2172  
  2173  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  2174  					})
  2175  				})
  2176  			})
  2177  
  2178  			When("the security group is bound to neither running nor staging", func() {
  2179  				BeforeEach(func() {
  2180  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  2181  						[]ccv2.SecurityGroup{},
  2182  						ccv2.Warnings{"warning-7", "warning-8"},
  2183  						nil,
  2184  					)
  2185  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  2186  						[]ccv2.SecurityGroup{},
  2187  						ccv2.Warnings{"warning-9", "warning-10"},
  2188  						nil,
  2189  					)
  2190  				})
  2191  
  2192  				When("no errors are encountered", func() {
  2193  					It("returns all warnings", func() {
  2194  						Expect(warnings).To(ConsistOf([]string{
  2195  							"warning-1",
  2196  							"warning-2",
  2197  							"warning-3",
  2198  							"warning-4",
  2199  							"warning-5",
  2200  							"warning-6",
  2201  							"warning-7",
  2202  							"warning-8",
  2203  							"warning-9",
  2204  							"warning-10",
  2205  						}))
  2206  						Expect(err).ToNot(HaveOccurred())
  2207  
  2208  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  2209  						spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  2210  						Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  2211  						Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  2212  							Type:     constant.NameFilter,
  2213  							Operator: constant.EqualOperator,
  2214  							Values:   []string{"some-security-group"},
  2215  						}}))
  2216  
  2217  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  2218  						spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  2219  						Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  2220  						Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  2221  							Type:     constant.NameFilter,
  2222  							Operator: constant.EqualOperator,
  2223  							Values:   []string{"some-security-group"},
  2224  						}}))
  2225  
  2226  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  2227  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  2228  					})
  2229  				})
  2230  			})
  2231  
  2232  			When("the security group is bound to staging", func() {
  2233  				BeforeEach(func() {
  2234  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  2235  						[]ccv2.SecurityGroup{},
  2236  						ccv2.Warnings{"warning-7", "warning-8"},
  2237  						nil,
  2238  					)
  2239  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  2240  						[]ccv2.SecurityGroup{
  2241  							{
  2242  								Name: "some-security-group",
  2243  								GUID: "some-security-group-guid",
  2244  							},
  2245  						},
  2246  						ccv2.Warnings{"warning-9", "warning-10"},
  2247  						nil,
  2248  					)
  2249  				})
  2250  
  2251  				It("returns all warnings and a SecurityGroupNotBoundError", func() {
  2252  					Expect(warnings).To(ConsistOf([]string{
  2253  						"warning-1",
  2254  						"warning-2",
  2255  						"warning-3",
  2256  						"warning-4",
  2257  						"warning-5",
  2258  						"warning-6",
  2259  						"warning-7",
  2260  						"warning-8",
  2261  						"warning-9",
  2262  						"warning-10",
  2263  					}))
  2264  					Expect(err).To(MatchError(actionerror.SecurityGroupNotBoundError{
  2265  						Name:      "some-security-group",
  2266  						Lifecycle: lifecycle,
  2267  					}))
  2268  
  2269  					Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  2270  					spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  2271  					Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  2272  					Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  2273  						Type:     constant.NameFilter,
  2274  						Operator: constant.EqualOperator,
  2275  						Values:   []string{"some-security-group"},
  2276  					}}))
  2277  
  2278  					Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  2279  					spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  2280  					Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  2281  					Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  2282  						Type:     constant.NameFilter,
  2283  						Operator: constant.EqualOperator,
  2284  						Values:   []string{"some-security-group"},
  2285  					}}))
  2286  
  2287  					Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  2288  					Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  2289  				})
  2290  			})
  2291  		})
  2292  
  2293  		When("the requested lifecycle is staging", func() {
  2294  			BeforeEach(func() {
  2295  				lifecycle = constant.SecurityGroupLifecycleStaging
  2296  
  2297  				fakeCloudControllerClient.GetSecurityGroupsReturns(
  2298  					[]ccv2.SecurityGroup{{
  2299  						Name: "some-security-group",
  2300  						GUID: "some-security-group-guid",
  2301  					}},
  2302  					ccv2.Warnings{"warning-1", "warning-2"},
  2303  					nil)
  2304  				fakeCloudControllerClient.GetOrganizationsReturns(
  2305  					[]ccv2.Organization{{
  2306  						Name: "some-org",
  2307  						GUID: "some-org-guid",
  2308  					}},
  2309  					ccv2.Warnings{"warning-3", "warning-4"},
  2310  					nil)
  2311  				fakeCloudControllerClient.GetSpacesReturns(
  2312  					[]ccv2.Space{{
  2313  						Name: "some-space",
  2314  						GUID: "some-space-guid",
  2315  					}},
  2316  					ccv2.Warnings{"warning-5", "warning-6"},
  2317  					nil)
  2318  			})
  2319  
  2320  			When("the security group is bound to staging", func() {
  2321  				BeforeEach(func() {
  2322  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  2323  						[]ccv2.SecurityGroup{
  2324  							{
  2325  								Name: "some-security-group",
  2326  								GUID: "some-security-group-guid",
  2327  							},
  2328  						},
  2329  						ccv2.Warnings{"warning-7", "warning-8"},
  2330  						nil,
  2331  					)
  2332  				})
  2333  
  2334  				When("an error is encountered unbinding the security group the space", func() {
  2335  					var returnedError error
  2336  
  2337  					BeforeEach(func() {
  2338  						fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  2339  							[]ccv2.SecurityGroup{
  2340  								{
  2341  									Name: "some-security-group",
  2342  									GUID: "some-security-group-guid",
  2343  								},
  2344  							},
  2345  							ccv2.Warnings{"warning-7", "warning-8"},
  2346  							nil,
  2347  						)
  2348  						returnedError = errors.New("associate-space-error")
  2349  						fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceReturns(
  2350  							ccv2.Warnings{"warning-9", "warning-10"},
  2351  							returnedError)
  2352  					})
  2353  
  2354  					It("returns the error and all warnings", func() {
  2355  						Expect(warnings).To(ConsistOf([]string{
  2356  							"warning-1",
  2357  							"warning-2",
  2358  							"warning-3",
  2359  							"warning-4",
  2360  							"warning-5",
  2361  							"warning-6",
  2362  							"warning-7",
  2363  							"warning-8",
  2364  							"warning-9",
  2365  							"warning-10",
  2366  						}))
  2367  						Expect(err).To(MatchError(returnedError))
  2368  					})
  2369  				})
  2370  
  2371  				When("no errors are encountered", func() {
  2372  					BeforeEach(func() {
  2373  						fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceReturns(
  2374  							ccv2.Warnings{"warning-9", "warning-10"},
  2375  							nil)
  2376  					})
  2377  
  2378  					It("returns all warnings", func() {
  2379  						Expect(err).ToNot(HaveOccurred())
  2380  						Expect(warnings).To(ConsistOf([]string{
  2381  							"warning-1",
  2382  							"warning-2",
  2383  							"warning-3",
  2384  							"warning-4",
  2385  							"warning-5",
  2386  							"warning-6",
  2387  							"warning-7",
  2388  							"warning-8",
  2389  							"warning-9",
  2390  							"warning-10",
  2391  						}))
  2392  
  2393  						Expect(fakeCloudControllerClient.GetSecurityGroupsCallCount()).To(Equal(1))
  2394  						Expect(fakeCloudControllerClient.GetSecurityGroupsArgsForCall(0)).To(Equal([]ccv2.Filter{{
  2395  							Type:     constant.NameFilter,
  2396  							Operator: constant.EqualOperator,
  2397  							Values:   []string{"some-security-group"},
  2398  						}}))
  2399  
  2400  						Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
  2401  						Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal([]ccv2.Filter{{
  2402  							Type:     constant.NameFilter,
  2403  							Operator: constant.EqualOperator,
  2404  							Values:   []string{"some-org"},
  2405  						}}))
  2406  
  2407  						Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
  2408  						Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal([]ccv2.Filter{{
  2409  							Type:     constant.NameFilter,
  2410  							Operator: constant.EqualOperator,
  2411  							Values:   []string{"some-space"},
  2412  						}, {
  2413  							Type:     constant.OrganizationGUIDFilter,
  2414  							Operator: constant.EqualOperator,
  2415  							Values:   []string{"some-org-guid"},
  2416  						}}))
  2417  
  2418  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  2419  						spaceGUID, queries := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  2420  						Expect(spaceGUID).To(Equal("some-space-guid"))
  2421  						Expect(queries).To(Equal([]ccv2.Filter{{
  2422  							Type:     constant.NameFilter,
  2423  							Operator: constant.EqualOperator,
  2424  							Values:   []string{"some-security-group"},
  2425  						}}))
  2426  
  2427  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(0))
  2428  
  2429  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(1))
  2430  						securityGroupGUID, spaceGUID := fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceArgsForCall(0)
  2431  						Expect(securityGroupGUID).To(Equal("some-security-group-guid"))
  2432  						Expect(spaceGUID).To(Equal("some-space-guid"))
  2433  
  2434  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  2435  					})
  2436  				})
  2437  			})
  2438  
  2439  			When("the security group is bound to neither running nor staging", func() {
  2440  				BeforeEach(func() {
  2441  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  2442  						[]ccv2.SecurityGroup{},
  2443  						ccv2.Warnings{"warning-7", "warning-8"},
  2444  						nil,
  2445  					)
  2446  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  2447  						[]ccv2.SecurityGroup{},
  2448  						ccv2.Warnings{"warning-9", "warning-10"},
  2449  						nil,
  2450  					)
  2451  				})
  2452  
  2453  				When("no errors are encountered", func() {
  2454  					It("returns all warnings", func() {
  2455  						Expect(err).ToNot(HaveOccurred())
  2456  						Expect(warnings).To(ConsistOf([]string{
  2457  							"warning-1",
  2458  							"warning-2",
  2459  							"warning-3",
  2460  							"warning-4",
  2461  							"warning-5",
  2462  							"warning-6",
  2463  							"warning-7",
  2464  							"warning-8",
  2465  							"warning-9",
  2466  							"warning-10",
  2467  						}))
  2468  
  2469  						Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  2470  						spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  2471  						Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  2472  						Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  2473  							Type:     constant.NameFilter,
  2474  							Operator: constant.EqualOperator,
  2475  							Values:   []string{"some-security-group"},
  2476  						}}))
  2477  
  2478  						Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  2479  						spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  2480  						Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  2481  						Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  2482  							Type:     constant.NameFilter,
  2483  							Operator: constant.EqualOperator,
  2484  							Values:   []string{"some-security-group"},
  2485  						}}))
  2486  
  2487  						Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  2488  						Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  2489  					})
  2490  				})
  2491  			})
  2492  
  2493  			When("the security group is bound to running", func() {
  2494  				BeforeEach(func() {
  2495  					fakeCloudControllerClient.GetSpaceStagingSecurityGroupsReturns(
  2496  						[]ccv2.SecurityGroup{},
  2497  						ccv2.Warnings{"warning-7", "warning-8"},
  2498  						nil,
  2499  					)
  2500  					fakeCloudControllerClient.GetSpaceSecurityGroupsReturns(
  2501  						[]ccv2.SecurityGroup{
  2502  							{
  2503  								Name: "some-security-group",
  2504  								GUID: "some-security-group-guid",
  2505  							},
  2506  						},
  2507  						ccv2.Warnings{"warning-9", "warning-10"},
  2508  						nil,
  2509  					)
  2510  				})
  2511  
  2512  				It("returns all warnings and a SecurityGroupNotBoundError", func() {
  2513  					Expect(warnings).To(ConsistOf([]string{
  2514  						"warning-1",
  2515  						"warning-2",
  2516  						"warning-3",
  2517  						"warning-4",
  2518  						"warning-5",
  2519  						"warning-6",
  2520  						"warning-7",
  2521  						"warning-8",
  2522  						"warning-9",
  2523  						"warning-10",
  2524  					}))
  2525  
  2526  					Expect(err).To(MatchError(actionerror.SecurityGroupNotBoundError{
  2527  						Name:      "some-security-group",
  2528  						Lifecycle: lifecycle,
  2529  					}))
  2530  
  2531  					Expect(fakeCloudControllerClient.GetSpaceStagingSecurityGroupsCallCount()).To(Equal(1))
  2532  					spaceGUIDStaging, queriesStaging := fakeCloudControllerClient.GetSpaceStagingSecurityGroupsArgsForCall(0)
  2533  					Expect(spaceGUIDStaging).To(Equal("some-space-guid"))
  2534  					Expect(queriesStaging).To(Equal([]ccv2.Filter{{
  2535  						Type:     constant.NameFilter,
  2536  						Operator: constant.EqualOperator,
  2537  						Values:   []string{"some-security-group"},
  2538  					}}))
  2539  
  2540  					Expect(fakeCloudControllerClient.GetSpaceSecurityGroupsCallCount()).To(Equal(1))
  2541  					spaceGUIDRunning, queriesRunning := fakeCloudControllerClient.GetSpaceSecurityGroupsArgsForCall(0)
  2542  					Expect(spaceGUIDRunning).To(Equal("some-space-guid"))
  2543  					Expect(queriesRunning).To(Equal([]ccv2.Filter{{
  2544  						Type:     constant.NameFilter,
  2545  						Operator: constant.EqualOperator,
  2546  						Values:   []string{"some-security-group"},
  2547  					}}))
  2548  
  2549  					Expect(fakeCloudControllerClient.DeleteSecurityGroupStagingSpaceCallCount()).To(Equal(0))
  2550  					Expect(fakeCloudControllerClient.DeleteSecurityGroupSpaceCallCount()).To(Equal(0))
  2551  				})
  2552  			})
  2553  		})
  2554  	})
  2555  })