github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/api/cloudcontroller/ccv2/domain_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Domain", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetSharedDomain", func() {
    22  		Context("when the shared domain exists", func() {
    23  			BeforeEach(func() {
    24  				response := `{
    25  						"metadata": {
    26  							"guid": "shared-domain-guid",
    27  							"updated_at": null
    28  						},
    29  						"entity": {
    30  							"name": "shared-domain-1.com",
    31  							"router_group_guid": "some-router-group-guid",
    32  							"router_group_type": "http"
    33  						}
    34  				}`
    35  				server.AppendHandlers(
    36  					CombineHandlers(
    37  						VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"),
    38  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    39  					),
    40  				)
    41  			})
    42  
    43  			It("returns the shared domain and all warnings", func() {
    44  				domain, warnings, err := client.GetSharedDomain("shared-domain-guid")
    45  				Expect(err).NotTo(HaveOccurred())
    46  				Expect(domain).To(Equal(Domain{
    47  					Name:            "shared-domain-1.com",
    48  					GUID:            "shared-domain-guid",
    49  					RouterGroupGUID: "some-router-group-guid",
    50  					RouterGroupType: constant.HTTPRouterGroup,
    51  					Type:            constant.SharedDomain,
    52  				}))
    53  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    54  			})
    55  		})
    56  
    57  		Context("when the shared domain does not exist", func() {
    58  			BeforeEach(func() {
    59  				response := `{
    60  					"code": 130002,
    61  					"description": "The domain could not be found: shared-domain-guid",
    62  					"error_code": "CF-DomainNotFound"
    63  				}`
    64  				server.AppendHandlers(
    65  					CombineHandlers(
    66  						VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"),
    67  						RespondWith(http.StatusNotFound, response),
    68  					),
    69  				)
    70  			})
    71  
    72  			It("returns an error", func() {
    73  				domain, _, err := client.GetSharedDomain("shared-domain-guid")
    74  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
    75  					Message: "The domain could not be found: shared-domain-guid",
    76  				}))
    77  				Expect(domain).To(Equal(Domain{}))
    78  			})
    79  		})
    80  	})
    81  
    82  	Describe("GetPrivateDomain", func() {
    83  		Context("when the private domain exists", func() {
    84  			BeforeEach(func() {
    85  				response := `{
    86  						"metadata": {
    87  							"guid": "private-domain-guid",
    88  							"updated_at": null
    89  						},
    90  						"entity": {
    91  							"name": "private-domain-1.com"
    92  						}
    93  				}`
    94  				server.AppendHandlers(
    95  					CombineHandlers(
    96  						VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"),
    97  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    98  					),
    99  				)
   100  			})
   101  
   102  			It("returns the private domain and all warnings", func() {
   103  				domain, warnings, err := client.GetPrivateDomain("private-domain-guid")
   104  				Expect(err).NotTo(HaveOccurred())
   105  				Expect(domain).To(Equal(Domain{
   106  					Name: "private-domain-1.com",
   107  					GUID: "private-domain-guid",
   108  					Type: constant.PrivateDomain,
   109  				}))
   110  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   111  			})
   112  		})
   113  
   114  		Context("when the private domain does not exist", func() {
   115  			BeforeEach(func() {
   116  				response := `{
   117  					"code": 130002,
   118  					"description": "The domain could not be found: private-domain-guid",
   119  					"error_code": "CF-DomainNotFound"
   120  				}`
   121  				server.AppendHandlers(
   122  					CombineHandlers(
   123  						VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"),
   124  						RespondWith(http.StatusNotFound, response),
   125  					),
   126  				)
   127  			})
   128  
   129  			It("returns an error", func() {
   130  				domain, _, err := client.GetPrivateDomain("private-domain-guid")
   131  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   132  					Message: "The domain could not be found: private-domain-guid",
   133  				}))
   134  				Expect(domain).To(Equal(Domain{}))
   135  			})
   136  		})
   137  	})
   138  
   139  	Describe("GetPrivateDomains", func() {
   140  		Context("when the cloud controller does not return an error", func() {
   141  			BeforeEach(func() {
   142  				response1 := `{
   143  					"next_url": "/v2/private_domains?q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2",
   144  					"resources": [
   145  						{
   146  							"metadata": {
   147  								"guid": "domain-guid-1"
   148  							},
   149  							"entity": {
   150  								"name": "domain-name-1"
   151  							}
   152  						},
   153  						{
   154  							"metadata": {
   155  								"guid": "domain-guid-2"
   156  							},
   157  							"entity": {
   158  								"name": "domain-name-2"
   159  							}
   160  						}
   161  					]
   162  				}`
   163  				response2 := `{
   164  					"next_url": null,
   165  					"resources": [
   166  						{
   167  							"metadata": {
   168  								"guid": "domain-guid-3"
   169  							},
   170  							"entity": {
   171  								"name": "domain-name-3"
   172  							}
   173  						},
   174  						{
   175  							"metadata": {
   176  								"guid": "domain-guid-4"
   177  							},
   178  							"entity": {
   179  								"name": "domain-name-4"
   180  							}
   181  						}
   182  					]
   183  				}`
   184  				server.AppendHandlers(
   185  					CombineHandlers(
   186  						VerifyRequest(http.MethodGet, "/v2/private_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4"),
   187  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   188  					),
   189  				)
   190  				server.AppendHandlers(
   191  					CombineHandlers(
   192  						VerifyRequest(http.MethodGet, "/v2/private_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2"),
   193  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   194  					),
   195  				)
   196  			})
   197  
   198  			It("returns the private domains and warnings", func() {
   199  				domains, warnings, err := client.GetPrivateDomains(Filter{
   200  					Type:     constant.NameFilter,
   201  					Operator: constant.InOperator,
   202  					Values:   []string{"domain-name-1", "domain-name-2", "domain-name-3", "domain-name-4"},
   203  				})
   204  				Expect(err).NotTo(HaveOccurred())
   205  				Expect(domains).To(Equal([]Domain{
   206  					{
   207  						GUID: "domain-guid-1",
   208  						Name: "domain-name-1",
   209  						Type: constant.PrivateDomain,
   210  					},
   211  					{
   212  						GUID: "domain-guid-2",
   213  						Name: "domain-name-2",
   214  						Type: constant.PrivateDomain,
   215  					},
   216  					{
   217  						GUID: "domain-guid-3",
   218  						Name: "domain-name-3",
   219  						Type: constant.PrivateDomain,
   220  					},
   221  					{
   222  						GUID: "domain-guid-4",
   223  						Name: "domain-name-4",
   224  						Type: constant.PrivateDomain,
   225  					},
   226  				}))
   227  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   228  			})
   229  		})
   230  
   231  		Context("when the cloud controller returns an error", func() {
   232  			BeforeEach(func() {
   233  				response := `{
   234  					"code": 1,
   235  					"description": "some error description",
   236  					"error_code": "CF-SomeError"
   237  				}`
   238  				server.AppendHandlers(
   239  					CombineHandlers(
   240  						VerifyRequest(http.MethodGet, "/v2/private_domains"),
   241  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   242  					),
   243  				)
   244  			})
   245  
   246  			It("returns the warnings and error", func() {
   247  				domains, warnings, err := client.GetPrivateDomains()
   248  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   249  					V2ErrorResponse: ccerror.V2ErrorResponse{
   250  						Code:        1,
   251  						Description: "some error description",
   252  						ErrorCode:   "CF-SomeError",
   253  					},
   254  					ResponseCode: http.StatusTeapot,
   255  				}))
   256  				Expect(domains).To(Equal([]Domain{}))
   257  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   258  			})
   259  		})
   260  	})
   261  
   262  	Describe("GetSharedDomains", func() {
   263  		Context("when the cloud controller does not return an error", func() {
   264  			BeforeEach(func() {
   265  				response1 := `{
   266  					"next_url": "/v2/shared_domains?q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2",
   267  					"resources": [
   268  						{
   269  							"metadata": {
   270  								"guid": "domain-guid-1"
   271  							},
   272  							"entity": {
   273  								"name": "domain-name-1",
   274  								"router_group_guid": "some-router-group-guid-1",
   275  								"router_group_type": "http"
   276  							}
   277  						},
   278  						{
   279  							"metadata": {
   280  								"guid": "domain-guid-2"
   281  							},
   282  							"entity": {
   283  								"name": "domain-name-2",
   284  								"router_group_guid": "some-router-group-guid-2",
   285  								"router_group_type": "http"
   286  							}
   287  						}
   288  					]
   289  				}`
   290  				response2 := `{
   291  					"next_url": null,
   292  					"resources": [
   293  						{
   294  							"metadata": {
   295  								"guid": "domain-guid-3"
   296  							},
   297  							"entity": {
   298  								"name": "domain-name-3",
   299  								"router_group_guid": "some-router-group-guid-3",
   300  								"router_group_type": "http"
   301  							}
   302  						},
   303  						{
   304  							"metadata": {
   305  								"guid": "domain-guid-4"
   306  							},
   307  							"entity": {
   308  								"name": "domain-name-4",
   309  								"router_group_guid": "some-router-group-guid-4",
   310  								"router_group_type": "http"
   311  							}
   312  						}
   313  					]
   314  				}`
   315  				server.AppendHandlers(
   316  					CombineHandlers(
   317  						VerifyRequest(http.MethodGet, "/v2/shared_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4"),
   318  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   319  					),
   320  				)
   321  				server.AppendHandlers(
   322  					CombineHandlers(
   323  						VerifyRequest(http.MethodGet, "/v2/shared_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2"),
   324  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   325  					),
   326  				)
   327  			})
   328  
   329  			It("returns the shared domain and warnings", func() {
   330  				domains, warnings, err := client.GetSharedDomains(Filter{
   331  					Type:     constant.NameFilter,
   332  					Operator: constant.InOperator,
   333  					Values:   []string{"domain-name-1", "domain-name-2", "domain-name-3", "domain-name-4"},
   334  				})
   335  				Expect(err).NotTo(HaveOccurred())
   336  				Expect(domains).To(Equal([]Domain{
   337  					{
   338  						GUID:            "domain-guid-1",
   339  						Name:            "domain-name-1",
   340  						RouterGroupGUID: "some-router-group-guid-1",
   341  						RouterGroupType: constant.HTTPRouterGroup,
   342  						Type:            constant.SharedDomain,
   343  					},
   344  					{
   345  						GUID:            "domain-guid-2",
   346  						Name:            "domain-name-2",
   347  						RouterGroupGUID: "some-router-group-guid-2",
   348  						RouterGroupType: constant.HTTPRouterGroup,
   349  						Type:            constant.SharedDomain,
   350  					},
   351  					{
   352  						GUID:            "domain-guid-3",
   353  						Name:            "domain-name-3",
   354  						RouterGroupGUID: "some-router-group-guid-3",
   355  						RouterGroupType: constant.HTTPRouterGroup,
   356  						Type:            constant.SharedDomain,
   357  					},
   358  					{
   359  						GUID:            "domain-guid-4",
   360  						Name:            "domain-name-4",
   361  						RouterGroupGUID: "some-router-group-guid-4",
   362  						RouterGroupType: constant.HTTPRouterGroup,
   363  						Type:            constant.SharedDomain,
   364  					},
   365  				}))
   366  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   367  			})
   368  		})
   369  
   370  		Context("when the cloud controller returns an error", func() {
   371  			BeforeEach(func() {
   372  				response := `{
   373  					"code": 1,
   374  					"description": "some error description",
   375  					"error_code": "CF-SomeError"
   376  				}`
   377  				server.AppendHandlers(
   378  					CombineHandlers(
   379  						VerifyRequest(http.MethodGet, "/v2/shared_domains"),
   380  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   381  					),
   382  				)
   383  			})
   384  
   385  			It("returns the warnings and error", func() {
   386  				domains, warnings, err := client.GetSharedDomains()
   387  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   388  					V2ErrorResponse: ccerror.V2ErrorResponse{
   389  						Code:        1,
   390  						Description: "some error description",
   391  						ErrorCode:   "CF-SomeError",
   392  					},
   393  					ResponseCode: http.StatusTeapot,
   394  				}))
   395  				Expect(domains).To(Equal([]Domain{}))
   396  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   397  			})
   398  		})
   399  	})
   400  
   401  	Describe("GetOrganizationPrivateDomains", func() {
   402  		Context("when the cloud controller does not return an error", func() {
   403  			BeforeEach(func() {
   404  				response1 := `{
   405  					"next_url": "/v2/organizations/some-org-guid/private_domains?page=2",
   406  					"resources": [
   407  						{
   408  							"metadata": {
   409  								"guid": "private-domain-guid-1"
   410  							},
   411  							"entity": {
   412  								"name": "private-domain-name-1"
   413  							}
   414  						},
   415  						{
   416  							"metadata": {
   417  								"guid": "private-domain-guid-2"
   418  							},
   419  							"entity": {
   420  								"name": "private-domain-name-2"
   421  							}
   422  						}
   423  					]
   424  				}`
   425  				response2 := `{
   426  					"next_url": null,
   427  					"resources": [
   428  						{
   429  							"metadata": {
   430  								"guid": "private-domain-guid-3"
   431  							},
   432  							"entity": {
   433  								"name": "private-domain-name-3"
   434  							}
   435  						},
   436  						{
   437  							"metadata": {
   438  								"guid": "private-domain-guid-4"
   439  							},
   440  							"entity": {
   441  								"name": "private-domain-name-4"
   442  							}
   443  						}
   444  					]
   445  				}`
   446  				server.AppendHandlers(
   447  					CombineHandlers(
   448  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"),
   449  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   450  					),
   451  				)
   452  				server.AppendHandlers(
   453  					CombineHandlers(
   454  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "page=2"),
   455  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   456  					),
   457  				)
   458  			})
   459  
   460  			It("returns the domains and warnings", func() {
   461  				domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid")
   462  				Expect(err).NotTo(HaveOccurred())
   463  				Expect(domains).To(Equal([]Domain{
   464  					{
   465  						Name: "private-domain-name-1",
   466  						GUID: "private-domain-guid-1",
   467  						Type: constant.PrivateDomain,
   468  					},
   469  					{
   470  						Name: "private-domain-name-2",
   471  						GUID: "private-domain-guid-2",
   472  						Type: constant.PrivateDomain,
   473  					},
   474  					{
   475  						Name: "private-domain-name-3",
   476  						GUID: "private-domain-guid-3",
   477  						Type: constant.PrivateDomain,
   478  					},
   479  					{
   480  						Name: "private-domain-name-4",
   481  						GUID: "private-domain-guid-4",
   482  						Type: constant.PrivateDomain,
   483  					},
   484  				}))
   485  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   486  			})
   487  		})
   488  
   489  		Context("when the client includes includes query parameters for name", func() {
   490  			It("it includes the query parameters in the request", func() {
   491  				server.AppendHandlers(
   492  					CombineHandlers(
   493  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "q=name:private-domain-name"),
   494  						RespondWith(http.StatusOK, ""),
   495  					),
   496  				)
   497  
   498  				client.GetOrganizationPrivateDomains("some-org-guid", Filter{
   499  					Type:     constant.NameFilter,
   500  					Operator: constant.EqualOperator,
   501  					Values:   []string{"private-domain-name"},
   502  				})
   503  			})
   504  		})
   505  
   506  		Context("when the cloud controller returns an error", func() {
   507  			BeforeEach(func() {
   508  				response := `{
   509  					   "description": "The organization could not be found: glah",
   510  					   "error_code": "CF-OrganizationNotFound",
   511  					   "code": 30003
   512  				}`
   513  				server.AppendHandlers(
   514  					CombineHandlers(
   515  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"),
   516  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   517  					),
   518  				)
   519  			})
   520  
   521  			It("returns the warnings and error", func() {
   522  				domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid")
   523  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
   524  					Message: "The organization could not be found: glah",
   525  				}))
   526  				Expect(domains).To(Equal([]Domain{}))
   527  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   528  			})
   529  		})
   530  	})
   531  })