From 9d4f67d885158c9a8d21bb946fd5cfddd4d4d887 Mon Sep 17 00:00:00 2001 From: Xie Zheng Date: Mon, 23 Dec 2024 17:57:14 +0800 Subject: [PATCH] Add StoragePolicy and ContentLibrary when creating VC Namespace --- test/e2e/framework.go | 14 +++++++++ test/e2e/vclient.go | 73 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/test/e2e/framework.go b/test/e2e/framework.go index 477a14de6..1d440c176 100644 --- a/test/e2e/framework.go +++ b/test/e2e/framework.go @@ -307,9 +307,23 @@ func (data *TestData) createVCNamespace(namespace string) error { }() svID, _ := data.vcClient.getSupervisorID() + storagePolicyID, _ := data.vcClient.getStoragePolicyID() + log.Info("", "storagePolicyID", storagePolicyID) + contentLibraryID, _ := data.vcClient.getContentLibraryID() + log.Info("", "contentLibraryID", contentLibraryID) vcNamespace := &VCNamespaceCreateSpec{ Supervisor: svID, Namespace: namespace, + StorageSpecs: []InstancesStorageSpec{ + { + Policy: storagePolicyID, + }, + }, + ContentLibraries: []InstancesContentLibrarySpec{ + { + ContentLibrary: contentLibraryID, + }, + }, NetworkSpec: InstancesNetworkConfigInfo{ NetworkProvider: "NSX_VPC", VpcNetwork: InstancesVpcNetworkInfo{ diff --git a/test/e2e/vclient.go b/test/e2e/vclient.go index 0f47194bd..4f121cad9 100644 --- a/test/e2e/vclient.go +++ b/test/e2e/vclient.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "strings" "sync" "time" @@ -27,6 +28,16 @@ type supervisorInfo struct { K8sStatus string `json:"kubernetes_status"` } +type storagePolicyInfo struct { + Name string `json:"name"` + Description string `json:"description"` + Policy string `json:"policy"` +} + +type contentLibraryInfo struct { + ID string `json:"policy"` +} + type supervisorSummary struct { ID string `json:"supervisor"` Info supervisorInfo `json:"info"` @@ -52,10 +63,23 @@ type InstancesNetworkConfigInfo struct { VpcNetwork InstancesVpcNetworkInfo `json:"vpc_network"` } +type InstancesStorageSpec struct { + Policy string + Limit *int64 +} + +type InstancesContentLibrarySpec struct { + ContentLibrary string + Writable *bool + AllowImport *bool +} + type VCNamespaceCreateSpec struct { - Supervisor string `json:"supervisor"` - Namespace string `json:"namespace"` - NetworkSpec InstancesNetworkConfigInfo `json:"network_spec"` + Supervisor string `json:"supervisor"` + Namespace string `json:"namespace"` + NetworkSpec InstancesNetworkConfigInfo `json:"network_spec"` + StorageSpecs []InstancesStorageSpec `json:"storage_specs"` + ContentLibraries []InstancesContentLibrarySpec `json:"content_libraries"` } type VCNamespaceGetInfo struct { @@ -150,6 +174,48 @@ func (c *vcClient) getSupervisorID() (string, error) { return "", fmt.Errorf("no valid supervisor found on vCenter") } +func (c *vcClient) getStoragePolicyID() (string, error) { + urlPath := "/api/vcenter/storage/policies" + request, err := c.prepareRequest(http.MethodGet, urlPath, nil) + if err != nil { + return "", err + } + var response struct { + Items []storagePolicyInfo `json:"items"` + } + if _, err = c.handleRequest(request, &response); err != nil { + return "", err + } + + for _, po := range response.Items { + if strings.Contains(po.Name, "global") || strings.Contains(po.Name, "local") { + return po.Name, nil + } + } + return "", fmt.Errorf("no valid storage policy found on vCenter") +} + +func (c *vcClient) getContentLibraryID() (string, error) { + urlPath := "/api/vcenter/content/library" + request, err := c.prepareRequest(http.MethodGet, urlPath, nil) + if err != nil { + return "", err + } + var response struct { + Items []contentLibraryInfo `json:"items"` + } + if _, err = c.handleRequest(request, &response); err != nil { + return "", err + } + + for _, cl := range response.Items { + if cl.ID != "" { + return cl.ID, nil + } + } + return "", fmt.Errorf("no valid content library found on vCenter") +} + func (c *vcClient) createNamespaceWithPreCreatedVPC(namespace string, vpcPath string, supervisorID string) error { vcNamespace := createVCNamespaceSpec(namespace, supervisorID, vpcPath) data, err := json.Marshal(vcNamespace) @@ -208,6 +274,7 @@ func createVCNamespaceSpec(namespace string, svID string, vpcPath string) *VCNam func (c *vcClient) prepareRequest(method string, urlPath string, data []byte) (*http.Request, error) { url := fmt.Sprintf("%s://%s%s", c.url.Scheme, c.url.Host, urlPath) + log.Info("Request URL: ", url) req, err := http.NewRequest(method, url, bytes.NewBuffer(data)) if err != nil { return nil, err