diff options
Diffstat (limited to 'src/inventory')
-rw-r--r-- | src/inventory/utils/util.go | 119 | ||||
-rw-r--r-- | src/inventory/utils/util_test.go | 126 |
2 files changed, 245 insertions, 0 deletions
diff --git a/src/inventory/utils/util.go b/src/inventory/utils/util.go new file mode 100644 index 00000000..8d204f92 --- /dev/null +++ b/src/inventory/utils/util.go @@ -0,0 +1,119 @@ +/* +Copyright 2020 Tech Mahindra. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package utils + +import ( + con "github.com/onap/multicloud-k8s/src/inventory/constants" + k8splugin "github.com/onap/multicloud-k8s/src/k8splugin/internal/app" + "net/http" + "os" + "reflect" +) + +/* Building relationship json to attach vserver details to vf-module*/ +func BuildRelationshipDataForVFModule(vserverName, vserverID, cloudOwner, cloudRegion, tenantId string) con.RelationList { + + rl := con.RelationList{"vserver", "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegion + "/tenants/tenant/" + tenantId + "/vservers/vserver/" + vserverID, []con.RData{con.RData{"cloud-region.cloud-owner", cloudOwner}, + con.RData{"cloud-region.cloud-region-id", cloudRegion}, + con.RData{"tenant.tenant-id", tenantId}, + con.RData{"vserver.vserver-id", vserverID}}, + []con.Property{con.Property{"vserver.vserver-name", vserverName}}} + + return rl + +} + +func ParseListInstanceResponse(rlist []k8splugin.InstanceMiniResponse) []string { + + var resourceIdList []string + + //assume there is only one resource created + for _, result := range rlist { + + resourceIdList = append(resourceIdList, result.ID) + } + + return resourceIdList +} + +/* Parse status api response to pull required information like Pod name, Profile name, namespace, ip details, vnf-id and vf-module-id*/ +func ParseStatusInstanceResponse(instanceStatusses []k8splugin.InstanceStatus) []con.PodInfoToAAI { + + var infoToAAI []con.PodInfoToAAI + + for _, instanceStatus := range instanceStatusses { + + var podInfo con.PodInfoToAAI + + sa := reflect.ValueOf(&instanceStatus).Elem() + typeOf := sa.Type() + for i := 0; i < sa.NumField(); i++ { + f := sa.Field(i) + if typeOf.Field(i).Name == "Request" { + request := f.Interface() + if ireq, ok := request.(k8splugin.InstanceRequest); ok { + podInfo.VserverName2 = ireq.ProfileName + podInfo.CloudRegion = ireq.CloudRegion + + for key, value := range ireq.Labels { + if key == "generic-vnf-id" { + + podInfo.VnfId = value + + } + if key == "vfmodule-id" { + + podInfo.VfmId = value + + } + } + + } else { + //fmt.Printf("it's not a InstanceRequest \n") + } + } + + if typeOf.Field(i).Name == "PodStatuses" { + ready := f.Interface() + if pss, ok := ready.([]con.PodStatus); ok { + for _, ps := range pss { + podInfo.VserverName = ps.Name + podInfo.ProvStatus = ps.Namespace + } + + } else { + //fmt.Printf("it's not a InstanceRequest \n") + } + } + } + + infoToAAI = append(infoToAAI, podInfo) + + } + + return infoToAAI + +} + +/* this sets http headers to request object*/ +func SetRequestHeaders(req *http.Request) { + authorization := os.Getenv("authorization") + + req.Header.Set("X-FromAppId", con.XFromAppId) + req.Header.Set("Content-Type", con.ContentType) + req.Header.Set("Accept", con.Accept) + req.Header.Set("X-TransactionId", con.XTransactionId) + req.Header.Set("Authorization", authorization) + +} diff --git a/src/inventory/utils/util_test.go b/src/inventory/utils/util_test.go new file mode 100644 index 00000000..4dd0c133 --- /dev/null +++ b/src/inventory/utils/util_test.go @@ -0,0 +1,126 @@ +/* +Copyright 2020 Tech Mahindra. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package utils + +import ( + con "github.com/onap/multicloud-k8s/src/inventory/constants" + k8splugin "github.com/onap/multicloud-k8s/src/k8splugin/internal/app" + "testing" +) + +func TestBuildRelationshipDataForVFModule(t *testing.T) { + + relList := BuildRelationshipDataForVFModule("vs_name", "vs1234", "CO", "CR", "tenant1234") + + if relList.RelatedTo != "vserver" { + t.Error("Failed") + } + + if (relList.RelatedLink) != "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/CO/CR/tenants/tenant/tenant1234/vservers/vserver/vs1234" { + t.Error("Failed") + } + + rdadaList := relList.RelationshipData + + for _, rdata := range rdadaList { + + if rdata.RelationshipKey == "cloud-region.cloud-region-id" { + + if rdata.RelationshipValue != "CR" { + + t.Error("Failed") + + } + } + + if rdata.RelationshipKey == "tenant.tenant-id" { + + if rdata.RelationshipValue != "tenant1234" { + + t.Error("Failed") + + } + } + + if rdata.RelationshipKey == "vserver.vserver-id" { + + if rdata.RelationshipValue != "vs1234" { + + t.Error("Failed") + + } + } + + if rdata.RelationshipKey == "cloud-region.cloud-owner" { + + if rdata.RelationshipValue != "CO" { + + t.Error("Failed") + + } + } + + } + + propertyList := relList.RelatedToProperty + + for _, property := range propertyList { + + if property.PropertyKey == "vserver.vserver-name" { + + if property.PropertyValue != "vs_name" { + + t.Error("Failed") + + } + } + + } + +} + +func TestParseStatusInstanceResponse(t *testing.T) { + + var resourceIdList []k8splugin.InstanceStatus + + instanceRequest := k8splugin.InstanceRequest{"rb_name", "rb_version", "profile123456", "c_region", map[string]string{"generic-vnf-id": "123456789", "vf-module-id": "987654321"}} + instanceStatus := k8splugin.InstanceStatus{instanceRequest, true, 12, []con.PodStatus{con.PodStatus{"pod123", "onap", true, []string{"10.211.1.100", "10.211.1.101"}}, con.PodStatus{"pod456", "default", true, []string{"10.211.1.200", "10.211.1.201"}}}} + + resourceIdList = append(resourceIdList, instanceStatus) + + podInfoToAAI := ParseStatusInstanceResponse(resourceIdList) + + for _, podInfo := range podInfoToAAI { + + if podInfo.VserverName == "pod123" { + + t.Error("Failed") + + } + + if podInfo.VserverName2 == "default" { + + t.Error("Failed") + + } + + if podInfo.ProvStatus == "profile123456" { + + t.Error("Failed") + + } + + } + +} |