diff options
50 files changed, 3807 insertions, 2758 deletions
diff --git a/deployments/helm/v2/onap4k8s/README.md b/deployments/helm/v2/onap4k8s/README.md index 3ce49a5a..43213153 100644 --- a/deployments/helm/v2/onap4k8s/README.md +++ b/deployments/helm/v2/onap4k8s/README.md @@ -28,9 +28,9 @@ Pacakges helm charts in tar.gz format. All packages are in **dist/packages** dir **3. Deploy EMCO Packages for Databases and Services** -`$ helm install dist/packages/emco-db-0.1.0.tgz --name rel-db --namespace emco` +`$ helm install dist/packages/emco-db-0.1.0.tgz --name emco-db --namespace emco` -`$ helm install dist/packages/emco-services-0.1.0.tgz --name rel-services --namespace emco` +`$ helm install dist/packages/emco-services-0.1.0.tgz --name emco-services --namespace emco` **4. Deploy tools (Optional)** @@ -65,10 +65,11 @@ Optional if tools were installed After deleting the db package and before installing the package again following error happens: - `Error: release rel01 failed: object is being deleted: persistentvolumes "rel-emco-etcd-data-0" already exists` + `Error: release emco-db failed: object is being deleted: persistentvolumes "emco-db-emco-etcd-data-0" already exists` Workaround : - `kubectl edit persistentvolumes rel01-emco-etcd-data-0` + `kubectl edit persistentvolumes emco-db-emco-etcd-data-0 -n emco` + + and remover finalizers section - and remover finalizers section
\ No newline at end of file diff --git a/deployments/helm/v2/onap4k8s/ovnaction/templates/deployment.yaml b/deployments/helm/v2/onap4k8s/ovnaction/templates/deployment.yaml index 56c62699..261b6ef2 100644 --- a/deployments/helm/v2/onap4k8s/ovnaction/templates/deployment.yaml +++ b/deployments/helm/v2/onap4k8s/ovnaction/templates/deployment.yaml @@ -43,8 +43,8 @@ spec: args: [{{ .Values.args }}] workingDir: {{ .Values.workingDir }} ports: - - containerPort: {{ .Values.service.internalPort }} - containerPort: {{ .Values.serviceInternal.internalPort }} + - containerPort: {{ .Values.service.internalPort }} {{- if eq .Values.liveness.enabled true }} livenessProbe: tcpSocket: diff --git a/deployments/helm/v2/onap4k8s/ovnaction/templates/service.yaml b/deployments/helm/v2/onap4k8s/ovnaction/templates/service.yaml index e9e59d38..9bf887ed 100644 --- a/deployments/helm/v2/onap4k8s/ovnaction/templates/service.yaml +++ b/deployments/helm/v2/onap4k8s/ovnaction/templates/service.yaml @@ -26,16 +26,7 @@ metadata: spec: type: {{ .Values.service.type }} ports: - - name: {{ .Values.service.name }} - {{if eq .Values.service.type "NodePort" -}} - port: {{ .Values.service.internalPort }} - nodePort: {{ .Values.global.nodePortPrefixExt | default "302" }}{{ .Values.service.nodePort }} - {{- else -}} - port: {{ .Values.service.externalPort }} - targetPort: {{ .Values.service.internalPort }} - {{- end}} - protocol: TCP - - name: {{ .Values.serviceInternal.name }} + - name: {{ .Values.serviceInternal.portName }} {{if eq .Values.serviceInternal.type "NodePort" -}} port: {{ .Values.serviceInternal.internalPort }} nodePort: {{ .Values.global.nodePortPrefixExt | default "302" }}{{ .Values.serviceInternal.nodePort }} @@ -44,6 +35,15 @@ spec: targetPort: {{ .Values.serviceInternal.internalPort }} {{- end}} protocol: TCP + - name: {{ .Values.service.portName }} + {{if eq .Values.service.type "NodePort" -}} + port: {{ .Values.service.internalPort }} + nodePort: {{ .Values.global.nodePortPrefixExt | default "302" }}{{ .Values.service.nodePort }} + {{- else -}} + port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + {{- end}} + protocol: TCP selector: app: {{ include "common.name" . }} release: {{ .Release.Name }}
\ No newline at end of file diff --git a/deployments/helm/v2/onap4k8s/ovnaction/values.yaml b/deployments/helm/v2/onap4k8s/ovnaction/values.yaml index 1773592f..411e5d0e 100644 --- a/deployments/helm/v2/onap4k8s/ovnaction/values.yaml +++ b/deployments/helm/v2/onap4k8s/ovnaction/values.yaml @@ -57,17 +57,17 @@ readiness: service: type: NodePort name: ovnaction - portName: ovnaction - internalPort: 9071 - externalPort: 9071 + portName: http + internalPort: 9051 + externalPort: 9051 nodePort: 71 serviceInternal: type: NodePort name: internal portName: internal - internalPort: 9073 - externalPort: 9073 + internalPort: 9053 + externalPort: 9053 nodePort: 73 ingress: diff --git a/deployments/helm/v2/onap4k8s/rsync/resources/config/config.json b/deployments/helm/v2/onap4k8s/rsync/resources/config/config.json index 484bdf04..56744d63 100644 --- a/deployments/helm/v2/onap4k8s/rsync/resources/config/config.json +++ b/deployments/helm/v2/onap4k8s/rsync/resources/config/config.json @@ -2,5 +2,5 @@ "database-type": "mongo", "database-ip": "emco-mongo", "etcd-ip": "emco-etcd", - "service-port": "9017" + "service-port": "9041" }
\ No newline at end of file diff --git a/docs/emco_apis.yaml b/docs/emco_apis.yaml index 643ac7b0..419c1316 100644 --- a/docs/emco_apis.yaml +++ b/docs/emco_apis.yaml @@ -1284,7 +1284,7 @@ paths: requestBody: content: {} - /projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/destroy: + /projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/terminate: parameters: - $ref: '#/components/parameters/projectName' - $ref: '#/components/parameters/compositeAppName' @@ -1293,9 +1293,9 @@ paths: post: tags: - Deployment Lifecycle - summary: Destroy a Deployment - description: Destroy a Deployment - operationId: destroyDeploymentIntentGroup + summary: Terminate a Deployment + description: Terminate a Deployment + operationId: terminateDeploymentIntentGroup responses: '200': description: Success @@ -3277,4 +3277,4 @@ components: required: true schema: type: string - maxLength: 128
\ No newline at end of file + maxLength: 128 diff --git a/kud/tests/m3db-operator-test.sh b/kud/tests/m3db-operator-test.sh index 1962c3f3..d5ea90db 100755 --- a/kud/tests/m3db-operator-test.sh +++ b/kud/tests/m3db-operator-test.sh @@ -366,7 +366,7 @@ function deleteData { deleteOrchestratorData } function instantiate { - # call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${vfw_compositeapp_name}/${vfw_compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/approve" + call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${vfw_compositeapp_name}/${vfw_compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/approve" call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${operators_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/instantiate" } diff --git a/kud/tests/plugin_collection_v2.sh b/kud/tests/plugin_collection_v2.sh index 05ff4265..84f5ca27 100755 --- a/kud/tests/plugin_collection_v2.sh +++ b/kud/tests/plugin_collection_v2.sh @@ -47,12 +47,12 @@ composite_app_name="test_composite_app_collection" composite_app_description="test_project_description" composite_app_version="test_composite_app_version" app1_helm_path="$CSAR_DIR/$csar_id/collectd.tar.gz" -app2_helm_path="$CSAR_DIR/$csar_id/prometheus.tar.gz" +app2_helm_path="$CSAR_DIR/$csar_id/prometheus-operator.tar.gz" app1_profile_path="$CSAR_DIR/$csar_id/collectd_profile.tar.gz" -app2_profile_path="$CSAR_DIR/$csar_id/prometheus_profile.tar.gz" +app2_profile_path="$CSAR_DIR/$csar_id/prometheus-operator_profile.tar.gz" app1_name="collectd" -app2_name="prometheus" +app2_name="prometheus-operator" app1_desc="collectd_desc" app2_desc="prometheus_desc" @@ -467,7 +467,7 @@ payload="$(cat <<EOF "spec":{ "intent":{ "${genericPlacementIntent}":"${genericPlacementIntentName}", - "${hpaIntent}" : "${hpaControllerIntentName}", + "${hpaIntent}" : "${hpaControllerIntentName}", "${trafficIntent}" : "${trafficControllerIntentName}", "${CostBasedIntent}" : "${CostBasedIntentName}", "${OVNintent}" : "${OVNintentName}" @@ -563,5 +563,6 @@ call_api -d "${payload}" "${base_url}/controllers" #BEGIN: Instantiation print_msg "Getting the sorted templates for each of the apps.." +call_api -d "" "${base_url}/projects/${project_name}/composite-apps/${composite_app_name}/${composite_app_version}/deployment-intent-groups/${deploymentIntentGroupName}/approve" call_api -d "" "${base_url}/projects/${project_name}/composite-apps/${composite_app_name}/${composite_app_version}/deployment-intent-groups/${deploymentIntentGroupName}/instantiate" # END: Instantiation diff --git a/kud/tests/prometheus-test.sh b/kud/tests/prometheus-test.sh index d9c38a15..ca995ba0 100755 --- a/kud/tests/prometheus-test.sh +++ b/kud/tests/prometheus-test.sh @@ -580,7 +580,7 @@ function deleteData { # } function instantiate { - # call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${vfw_compositeapp_name}/${vfw_compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/approve" + call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${vfw_compositeapp_name}/${vfw_compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/approve" call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/instantiate" } diff --git a/kud/tests/sanity-check-for-v2.sh b/kud/tests/sanity-check-for-v2.sh new file mode 100755 index 00000000..d350f712 --- /dev/null +++ b/kud/tests/sanity-check-for-v2.sh @@ -0,0 +1,509 @@ +#!/bin/bash + +# Copyright 2020 Intel Corporation, Inc +# +# 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. +# + + +set -o errexit +set -o nounset +set -o pipefail + + +source _common_test.sh +source _functions.sh +#source _common.sh + + +base_url_orchestrator=${base_url_orchestrator:-"http://localhost:9015/v2"} +base_url_clm=${base_url_clm:-"http://localhost:9019/v2"} + + +CSAR_DIR="/opt/csar" +csar_id="cb009bfe-bbee-11e8-9766-525400435678" + + +app1_helm_path="$CSAR_DIR/$csar_id/prometheus-operator.tar.gz" +app1_profile_path="$CSAR_DIR/$csar_id/prometheus-operator_profile.tar.gz" +app2_helm_path="$CSAR_DIR/$csar_id/collectd.tar.gz" +app2_profile_path="$CSAR_DIR/$csar_id/collectd_profile.tar.gz" + +kubeConfigLocal="/home/otc/.kube/config" + + + +function populate_CSAR_composite_app_helm { + _checks_args "$1" + pushd "${CSAR_DIR}/$1" + print_msg "Create Helm Chart Archives for compositeApp" + rm -f *.tar.gz + tar -czf collectd.tar.gz -C $test_folder/vnfs/comp-app/collection/app1/helm . + tar -czf prometheus-operator.tar.gz -C $test_folder/vnfs/comp-app/collection/app2/helm . + tar -czf collectd_profile.tar.gz -C $test_folder/vnfs/comp-app/collection/app1/profile . + tar -czf prometheus-operator_profile.tar.gz -C $test_folder/vnfs/comp-app/collection/app2/profile . + popd +} + + +# ---------BEGIN: SET CLM DATA--------------- + +clusterprovidername="sanity-test-cluster-provider" +clusterproviderdata="$(cat<<EOF +{ + "metadata": { + "name": "$clusterprovidername", + "description": "description of $clusterprovidername", + "userData1": "$clusterprovidername user data 1", + "userData2": "$clusterprovidername user data 2" + } +} +EOF +)" + +clustername="LocalEdge1" +clusterdata="$(cat<<EOF +{ + "metadata": { + "name": "$clustername", + "description": "description of $clustername", + "userData1": "$clustername user data 1", + "userData2": "$clustername user data 2" + } +} +EOF +)" + + +labelname="LocalLabel" +labeldata="$(cat<<EOF +{"label-name": "$labelname"} +EOF +)" + + +# add the rsync controller entry +rsynccontrollername="rsync" +rsynccontrollerdata="$(cat<<EOF +{ + "metadata": { + "name": "rsync", + "description": "description of $rsynccontrollername controller", + "userData1": "user data 1 for $rsynccontrollername", + "userData2": "user data 2 for $rsynccontrollername" + }, + "spec": { + "host": "localhost", + "port": 9031 + } +} +EOF +)" + +# ------------END: SET CLM DATA-------------- + + +#-------------BEGIN:SET ORCH DATA------------------ + +# define a project +projectname="Sanity-Test-Project" +projectdata="$(cat<<EOF +{ + "metadata": { + "name": "$projectname", + "description": "description of $projectname controller", + "userData1": "$projectname user data 1", + "userData2": "$projectname user data 2" + } +} +EOF +)" + +# define a composite application +collection_compositeapp_name="CollectionCompositeApp" +compositeapp_version="v1" +compositeapp_data="$(cat <<EOF +{ + "metadata": { + "name": "${collection_compositeapp_name}", + "description": "description of ${collection_compositeapp_name}", + "userData1": "user data 1 for ${collection_compositeapp_name}", + "userData2": "user data 2 for ${collection_compositeapp_name}" + }, + "spec":{ + "version":"${compositeapp_version}" + } +} +EOF +)" + +# add app entries for the prometheus app into +# compositeApp + +prometheus_app_name="prometheus-operator" +prometheus_helm_chart=${app1_helm_path} + +prometheus_app_data="$(cat <<EOF +{ + "metadata": { + "name": "${prometheus_app_name}", + "description": "description for app ${prometheus_app_name}", + "userData1": "user data 2 for ${prometheus_app_name}", + "userData2": "user data 2 for ${prometheus_app_name}" + } +} +EOF +)" + +# add app entries for the collectd app into +# compositeApp + +collectd_app_name="collectd" +collectd_helm_chart=${app2_helm_path} + +collectd_app_data="$(cat <<EOF +{ + "metadata": { + "name": "${collectd_app_name}", + "description": "description for app ${collectd_app_name}", + "userData1": "user data 2 for ${collectd_app_name}", + "userData2": "user data 2 for ${collectd_app_name}" + } +} +EOF +)" + + +# Add the composite profile +collection_composite_profile_name="collection_composite-profile" +collection_composite_profile_data="$(cat <<EOF +{ + "metadata":{ + "name":"${collection_composite_profile_name}", + "description":"description of ${collection_composite_profile_name}", + "userData1":"user data 1 for ${collection_composite_profile_name}", + "userData2":"user data 2 for ${collection_composite_profile_name}" + } +} +EOF +)" + +# Add the prometheus profile data into collection profile data +prometheus_profile_name="prometheus-profile" +prometheus_profile_file=$app1_profile_path +prometheus_profile_data="$(cat <<EOF +{ + "metadata":{ + "name":"${prometheus_profile_name}", + "description":"description of ${prometheus_profile_name}", + "userData1":"user data 1 for ${prometheus_profile_name}", + "userData2":"user data 2 for ${prometheus_profile_name}" + }, + "spec":{ + "app-name": "${prometheus_app_name}" + } +} +EOF +)" + +# Add the collectd profile data into collection profile data +collectd_profile_name="collectd-profile" +collectd_profile_file=$app2_profile_path +collectd_profile_data="$(cat <<EOF +{ + "metadata":{ + "name":"${collectd_profile_name}", + "description":"description of ${collectd_profile_name}", + "userData1":"user data 1 for ${collectd_profile_name}", + "userData2":"user data 2 for ${collectd_profile_name}" + }, + "spec":{ + "app-name": "${collectd_app_name}" + } +} +EOF +)" + + +# define the generic placement intent +generic_placement_intent_name="test-generic-placement-intent" +generic_placement_intent_data="$(cat <<EOF +{ + "metadata":{ + "name":"${generic_placement_intent_name}", + "description":"${generic_placement_intent_name}", + "userData1":"${generic_placement_intent_name}", + "userData2":"${generic_placement_intent_name}" + }, + "spec":{ + "logical-cloud":"unused_logical_cloud" + } +} +EOF +)" + +# define app placement intent for prometheus +prometheus_placement_intent_name="prometheus-placement-intent" +prometheus_placement_intent_data="$(cat <<EOF +{ + "metadata":{ + "name":"${prometheus_placement_intent_name}", + "description":"description of ${prometheus_placement_intent_name}", + "userData1":"user data 1 for ${prometheus_placement_intent_name}", + "userData2":"user data 2 for ${prometheus_placement_intent_name}" + }, + "spec":{ + "app-name":"${prometheus_app_name}", + "intent":{ + "allOf":[ + { "provider-name":"${clusterprovidername}", + "cluster-label-name":"${labelname}" + } + ] + } + } +} +EOF +)" + +# define app placement intent for collectd +collectd_placement_intent_name="collectd-placement-intent" +collectd_placement_intent_data="$(cat <<EOF +{ + "metadata":{ + "name":"${collectd_placement_intent_name}", + "description":"description of ${collectd_placement_intent_name}", + "userData1":"user data 1 for ${collectd_placement_intent_name}", + "userData2":"user data 2 for ${collectd_placement_intent_name}" + }, + "spec":{ + "app-name":"${collectd_app_name}", + "intent":{ + "allOf":[ + { "provider-name":"${clusterprovidername}", + "cluster-label-name":"${labelname}" + } + ] + } + } +} +EOF +)" + + +# define a deployment intent group +release="test-collection" +deployment_intent_group_name="collection_deployment_intent_group" +deployment_intent_group_data="$(cat <<EOF +{ + "metadata":{ + "name":"${deployment_intent_group_name}", + "description":"descriptiont of ${deployment_intent_group_name}", + "userData1":"user data 1 for ${deployment_intent_group_name}", + "userData2":"user data 2 for ${deployment_intent_group_name}" + }, + "spec":{ + "profile":"${collection_composite_profile_name}", + "version":"${release}", + "override-values":[] + } +} +EOF +)" + +# define the intents to be used by the group +deployment_intents_in_group_name="collection_deploy_intents" +deployment_intents_in_group_data="$(cat <<EOF +{ + "metadata":{ + "name":"${deployment_intents_in_group_name}", + "description":"descriptionf of ${deployment_intents_in_group_name}", + "userData1":"user data 1 for ${deployment_intents_in_group_name}", + "userData2":"user data 2 for ${deployment_intents_in_group_name}" + }, + "spec":{ + "intent":{ + "genericPlacementIntent":"${generic_placement_intent_name}" + } + } +} +EOF +)" + + +#---------END: SET ORCH DATA-------------------- + + +function createOrchestratorData { + + print_msg "creating controller entries" + call_api -d "${rsynccontrollerdata}" "${base_url_orchestrator}/controllers" + print_msg "creating project entry" + call_api -d "${projectdata}" "${base_url_orchestrator}/projects" + + print_msg "creating collection composite app entry" + call_api -d "${compositeapp_data}" "${base_url_orchestrator}/projects/${projectname}/composite-apps" + + print_msg "adding prometheus app to the composite app" + call_api -F "metadata=${prometheus_app_data}" \ + -F "file=@${prometheus_helm_chart}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/apps" + + print_msg "adding collectd app to the composite app" + call_api -F "metadata=${collectd_app_data}" \ + -F "file=@${collectd_helm_chart}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/apps" + + print_msg "creating collection composite profile entry" + call_api -d "${collection_composite_profile_data}" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/composite-profiles" + + print_msg "adding prometheus app profiles to the composite profile" + call_api -F "metadata=${prometheus_profile_data}" \ + -F "file=@${prometheus_profile_file}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/composite-profiles/${collection_composite_profile_name}/profiles" + + print_msg "adding collectd app profiles to the composite profile" + call_api -F "metadata=${collectd_profile_data}" \ + -F "file=@${collectd_profile_file}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/composite-profiles/${collection_composite_profile_name}/profiles" + + print_msg "create the generic placement intent" + call_api -d "${generic_placement_intent_data}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/generic-placement-intents" + + print_msg "add the prometheus app placement intent to the generic placement intent" + call_api -d "${prometheus_placement_intent_data}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/generic-placement-intents/${generic_placement_intent_name}/app-intents" + + print_msg "add the collectd app placement intent to the generic placement intent" + call_api -d "${collectd_placement_intent_data}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/generic-placement-intents/${generic_placement_intent_name}/app-intents" + + + print_msg "create the deployment intent group" + call_api -d "${deployment_intent_group_data}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups" + call_api -d "${deployment_intents_in_group_data}" \ + "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/intents" + +} + +function deleteOrchestratorData { + + print_msg "Begin deleteOrchestratorData" + + delete_resource "${base_url_orchestrator}/controllers/${rsynccontrollername}" + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/intents/${deployment_intents_in_group_name}" + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}" + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/generic-placement-intents/${generic_placement_intent_name}/app-intents/${prometheus_placement_intent_name}" + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/generic-placement-intents/${generic_placement_intent_name}/app-intents/${collectd_placement_intent_name}" + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/generic-placement-intents/${generic_placement_intent_name}" + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/composite-profiles/${collection_composite_profile_name}/profiles/${prometheus_profile_name}" + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/composite-profiles/${collection_composite_profile_name}/profiles/${collectd_profile_name}" + + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/composite-profiles/${collection_composite_profile_name}" + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/apps/${prometheus_app_name}" + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/apps/${collectd_app_name}" + + + delete_resource "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}" + delete_resource "${base_url_orchestrator}/projects/${projectname}" + + print_msg "deleteOrchestratorData done" +} + + +function createClmData { + print_msg "Creating cluster provider and cluster" + call_api -d "${clusterproviderdata}" "${base_url_clm}/cluster-providers" + + call_api -H "Content-Type: multipart/form-data" -F "metadata=$clusterdata" -F "file=@$kubeConfigLocal" "${base_url_clm}/cluster-providers/${clusterprovidername}/clusters" + + call_api -d "${labeldata}" "${base_url_clm}/cluster-providers/${clusterprovidername}/clusters/${clustername}/labels" + + +} + +function deleteClmData { + print_msg "begin deleteClmData" + delete_resource "${base_url_clm}/cluster-providers/${clusterprovidername}/clusters/${clustername}/labels/${labelname}" + delete_resource "${base_url_clm}/cluster-providers/${clusterprovidername}/clusters/${clustername}" + delete_resource "${base_url_clm}/cluster-providers/${clusterprovidername}" + print_msg "deleteClmData done" +} + +function createData { + createClmData + createOrchestratorData +} + +function deleteData { + deleteClmData + deleteOrchestratorData +} + +function instantiate { + call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/approve" + call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/instantiate" +} + + +function terminateOrchData { + call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${collection_compositeapp_name}/${compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/terminate" +} + +# Setup + +function setup { + install_deps + populate_CSAR_composite_app_helm "$csar_id" +} + +function usage { + echo "" + echo " Usage: $0 start | stop" + echo "" + echo " start - creates the orchstrator and cluster management data, instantiates the resources for collectd and prometheus and then deploys them on the local cluster" + echo "" + echo " stop - terminates the resources for collectd and prometheus and uninstalls the compositeApp" + echo "" + exit +} + +if [ "$#" -ne 1 ] ; then + usage +fi + + +case "$1" in + "start" ) + deleteData + print_msg "deleting the data success" + createData + print_msg "creating the data success" + instantiate + print_msg "instantiate success" + ;; + "stop" ) + terminateOrchData + print_msg "terminated the resources" + ;; + *) usage ;; +esac diff --git a/kud/tests/vfw-test.sh b/kud/tests/vfw-test.sh index b14ad95b..f4f96b2e 100755 --- a/kud/tests/vfw-test.sh +++ b/kud/tests/vfw-test.sh @@ -976,7 +976,7 @@ function terminateVfw { } function instantiateVfw { - # call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${vfw_compositeapp_name}/${vfw_compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/approve" + call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${vfw_compositeapp_name}/${vfw_compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/approve" call_api -d "{ }" "${base_url_orchestrator}/projects/${projectname}/composite-apps/${vfw_compositeapp_name}/${vfw_compositeapp_version}/deployment-intent-groups/${deployment_intent_group_name}/instantiate" } diff --git a/src/clm/api/clusterhandler_test.go b/src/clm/api/clusterhandler_test.go index 4bbc91b1..076718df 100644 --- a/src/clm/api/clusterhandler_test.go +++ b/src/clm/api/clusterhandler_test.go @@ -28,8 +28,8 @@ import ( "testing" "github.com/onap/multicloud-k8s/src/clm/pkg/cluster" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" types "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -43,7 +43,7 @@ type mockClusterManager struct { ClusterProviderItems []cluster.ClusterProvider ClusterItems []cluster.Cluster ClusterContentItems []cluster.ClusterContent - ClusterContextItems []appcontext.AppContext + ClusterStateInfo []state.StateInfo ClusterLabelItems []cluster.ClusterLabel ClusterKvPairsItems []cluster.ClusterKvPairs ClusterList []string @@ -102,12 +102,12 @@ func (m *mockClusterManager) GetClusterContent(provider, name string) (cluster.C return m.ClusterContentItems[0], nil } -func (m *mockClusterManager) GetClusterContext(provider, name string) (appcontext.AppContext, string, error) { +func (m *mockClusterManager) GetClusterState(provider, name string) (state.StateInfo, error) { if m.Err != nil { - return appcontext.AppContext{}, "", m.Err + return state.StateInfo{}, m.Err } - return m.ClusterContextItems[0], "", nil + return m.ClusterStateInfo[0], nil } func (m *mockClusterManager) GetClusters(provider string) ([]cluster.Cluster, error) { diff --git a/src/clm/go.mod b/src/clm/go.mod index 0e655566..1d174b12 100644 --- a/src/clm/go.mod +++ b/src/clm/go.mod @@ -2,25 +2,24 @@ module github.com/onap/multicloud-k8s/src/clm require ( github.com/ghodss/yaml v1.0.0 - github.com/go-stack/stack v1.8.0 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/gorilla/handlers v1.3.0 - github.com/gorilla/mux v1.6.2 + github.com/gorilla/mux v1.7.3 github.com/k8snetworkplumbingwg/network-attachment-definition-client v0.0.0-20200127152046-0ee521d56061 - github.com/onap/multicloud-k8s/src/orchestrator v0.0.0-20200601021239-7959bd4c6fd4 + github.com/onap/multicloud-k8s/src/orchestrator v0.0.0-20200721211210-783ed87fb39a github.com/opencontainers/go-digest v1.0.0 // indirect - github.com/pkg/errors v0.8.1 - github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect - github.com/xdg/stringprep v1.0.0 // indirect - google.golang.org/grpc v1.27.1 + github.com/pkg/errors v0.9.1 + google.golang.org/grpc v1.28.0 gopkg.in/yaml.v2 v2.2.8 k8s.io/api v0.0.0-20190831074750-7364b6bdad65 - k8s.io/apimachinery v0.0.0-20190831074630-461753078381 + k8s.io/apimachinery v0.18.2 k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible k8s.io/kubernetes v1.14.1 ) replace ( + github.com/onap/multicloud-k8s/src/orchestrator => ../orchestrator + github.com/onap/multicloud-k8s/src/clm => ../clm k8s.io/api => k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8 k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d diff --git a/src/clm/pkg/cluster/cluster.go b/src/clm/pkg/cluster/cluster.go index ac7f31f7..9505bd97 100644 --- a/src/clm/pkg/cluster/cluster.go +++ b/src/clm/pkg/cluster/cluster.go @@ -17,9 +17,9 @@ package cluster import ( - appcontext "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -28,7 +28,7 @@ type clientDbInfo struct { storeName string // name of the mongodb collection to use for client documents tagMeta string // attribute key name for the json data of a client document tagContent string // attribute key name for the file data of a client document - tagContext string // attribute key name for context object in App Context + tagState string // attribute key name for StateInfo object in the cluster } // ClusterProvider contains the parameters needed for ClusterProviders @@ -101,7 +101,7 @@ type ClusterManager interface { CreateCluster(provider string, pr Cluster, qr ClusterContent) (Cluster, error) GetCluster(provider, name string) (Cluster, error) GetClusterContent(provider, name string) (ClusterContent, error) - GetClusterContext(provider, name string) (appcontext.AppContext, string, error) + GetClusterState(provider, name string) (state.StateInfo, error) GetClusters(provider string) ([]Cluster, error) GetClustersWithLabel(provider, label string) ([]string, error) DeleteCluster(provider, name string) error @@ -129,7 +129,7 @@ func NewClusterClient() *ClusterClient { storeName: "cluster", tagMeta: "clustermetadata", tagContent: "clustercontent", - tagContext: "clustercontext", + tagState: "stateInfo", }, } } @@ -254,6 +254,17 @@ func (v *ClusterClient) CreateCluster(provider string, p Cluster, q ClusterConte return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") } + // Add the stateInfo record + stateInfo := state.StateInfo{ + State: state.StateEnum.Created, + ContextId: "", + } + + err = db.DBconn.Insert(v.db.storeName, key, nil, v.db.tagState, stateInfo) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Creating cluster StateInfo") + } + return p, nil } @@ -309,31 +320,29 @@ func (v *ClusterClient) GetClusterContent(provider, name string) (ClusterContent return ClusterContent{}, pkgerrors.New("Error getting Cluster Content") } -// GetClusterContext returns the AppContext for corresponding provider and name -func (v *ClusterClient) GetClusterContext(provider, name string) (appcontext.AppContext, string, error) { +// GetClusterState returns the StateInfo structure for corresponding cluster provider and cluster +func (v *ClusterClient) GetClusterState(provider, name string) (state.StateInfo, error) { //Construct key and tag to select the entry key := ClusterKey{ ClusterProviderName: provider, ClusterName: name, } - value, err := db.DBconn.Find(v.db.storeName, key, v.db.tagContext) + result, err := db.DBconn.Find(v.db.storeName, key, v.db.tagState) if err != nil { - return appcontext.AppContext{}, "", pkgerrors.Wrap(err, "Get Cluster Context") + return state.StateInfo{}, pkgerrors.Wrap(err, "Get Cluster StateInfo") } - //value is a byte array - if value != nil { - ctxVal := string(value[0]) - var cc appcontext.AppContext - _, err = cc.LoadAppContext(ctxVal) + if result != nil { + s := state.StateInfo{} + err = db.DBconn.Unmarshal(result[0], &s) if err != nil { - return appcontext.AppContext{}, "", pkgerrors.Wrap(err, "Reinitializing Cluster AppContext") + return state.StateInfo{}, pkgerrors.Wrap(err, "Unmarshalling Cluster StateInfo") } - return cc, ctxVal, nil + return s, nil } - return appcontext.AppContext{}, "", pkgerrors.New("Error getting Cluster AppContext") + return state.StateInfo{}, pkgerrors.New("Error getting Cluster StateInfo") } // GetClusters returns all the Clusters for corresponding provider @@ -393,9 +402,9 @@ func (v *ClusterClient) DeleteCluster(provider, name string) error { ClusterProviderName: provider, ClusterName: name, } - _, _, err := v.GetClusterContext(provider, name) - if err == nil { - return pkgerrors.Errorf("Cannot delete cluster until context is deleted: %v, %v", provider, name) + s, err := v.GetClusterState(provider, name) + if err == nil && s.State == state.StateEnum.Applied { + return pkgerrors.Errorf("Cluster network intents must be terminated before it can be deleted: " + name) } err = db.DBconn.Remove(v.db.storeName, key) diff --git a/src/dcm/api/api.go b/src/dcm/api/api.go index 87ad77b5..de1d5c97 100644 --- a/src/dcm/api/api.go +++ b/src/dcm/api/api.go @@ -14,143 +14,142 @@ limitations under the License. package api import ( + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - - "github.com/gorilla/mux" + "github.com/gorilla/mux" ) // NewRouter creates a router that registers the various urls that are // supported func NewRouter( - logicalCloudClient module.LogicalCloudManager, - clusterClient module.ClusterManager, - userPermissionClient module.UserPermissionManager, - quotaClient module.QuotaManager, - keyValueClient module.KeyValueManager) *mux.Router { - - router := mux.NewRouter() - - // Set up Logical Cloud handler routes - if logicalCloudClient == nil { - logicalCloudClient = module.NewLogicalCloudClient() - } - - if clusterClient == nil { - clusterClient = module.NewClusterClient() - } - - if quotaClient == nil { - quotaClient = module.NewQuotaClient() - } - - logicalCloudHandler := logicalCloudHandler{client: logicalCloudClient, - clusterClient: clusterClient, - quotaClient: quotaClient, - } - lcRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() - lcRouter.HandleFunc( - "/logical-clouds", - logicalCloudHandler.createHandler).Methods("POST") - lcRouter.HandleFunc( - "/logical-clouds", - logicalCloudHandler.getHandler).Methods("GET") - lcRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}", - logicalCloudHandler.getHandler).Methods("GET") - lcRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}", - logicalCloudHandler.deleteHandler).Methods("DELETE") - lcRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}", - logicalCloudHandler.updateHandler).Methods("PUT") - lcRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/apply", - logicalCloudHandler.applyHandler).Methods("POST") - // To Do - // get kubeconfig - /*lcRouter.HandleFunc( - "/logical-clouds/{name}/kubeconfig?cluster-reference={cluster}", - logicalCloudHandler.getConfigHandler).Methods("GET") - //get status - lcRouter.HandleFunc( - "/logical-clouds/{name}/cluster-references/", - logicalCloudHandler.associateHandler).Methods("GET")*/ - - // Set up Cluster API - - clusterHandler := clusterHandler{client: clusterClient} - clusterRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() - clusterRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-references", - clusterHandler.createHandler).Methods("POST") - clusterRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-references", - clusterHandler.getHandler).Methods("GET") - clusterRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", - clusterHandler.getHandler).Methods("GET") - clusterRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", - clusterHandler.updateHandler).Methods("PUT") - clusterRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", - clusterHandler.deleteHandler).Methods("DELETE") - - // Set up User Permission API - if userPermissionClient == nil { - userPermissionClient = module.NewUserPermissionClient() - } - userPermissionHandler := userPermissionHandler{client: userPermissionClient} - upRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() - upRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/user-permissions", - userPermissionHandler.createHandler).Methods("POST") - upRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}", - userPermissionHandler.getHandler).Methods("GET") - upRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}", - userPermissionHandler.updateHandler).Methods("PUT") - upRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}", - userPermissionHandler.deleteHandler).Methods("DELETE") - - // Set up Quota API - - quotaHandler := quotaHandler{client: quotaClient} - quotaRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() - quotaRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-quotas", - quotaHandler.createHandler).Methods("POST") - quotaRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}", - quotaHandler.getHandler).Methods("GET") - quotaRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}", - quotaHandler.updateHandler).Methods("PUT") - quotaRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}", - quotaHandler.deleteHandler).Methods("DELETE") - - // Set up Key Value API - if keyValueClient == nil { - keyValueClient = module.NewKeyValueClient() - } - keyValueHandler := keyValueHandler{client: keyValueClient} - kvRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() - kvRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/kv-pairs", - keyValueHandler.createHandler).Methods("POST") - kvRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}", - keyValueHandler.getHandler).Methods("GET") - kvRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}", - keyValueHandler.updateHandler).Methods("PUT") - kvRouter.HandleFunc( - "/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}", - keyValueHandler.deleteHandler).Methods("DELETE") - return router + logicalCloudClient module.LogicalCloudManager, + clusterClient module.ClusterManager, + userPermissionClient module.UserPermissionManager, + quotaClient module.QuotaManager, + keyValueClient module.KeyValueManager) *mux.Router { + + router := mux.NewRouter() + + // Set up Logical Cloud handler routes + if logicalCloudClient == nil { + logicalCloudClient = module.NewLogicalCloudClient() + } + + if clusterClient == nil { + clusterClient = module.NewClusterClient() + } + + if quotaClient == nil { + quotaClient = module.NewQuotaClient() + } + + logicalCloudHandler := logicalCloudHandler{client: logicalCloudClient, + clusterClient: clusterClient, + quotaClient: quotaClient, + } + lcRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() + lcRouter.HandleFunc( + "/logical-clouds", + logicalCloudHandler.createHandler).Methods("POST") + lcRouter.HandleFunc( + "/logical-clouds", + logicalCloudHandler.getHandler).Methods("GET") + lcRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}", + logicalCloudHandler.getHandler).Methods("GET") + lcRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}", + logicalCloudHandler.deleteHandler).Methods("DELETE") + lcRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}", + logicalCloudHandler.updateHandler).Methods("PUT") + lcRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/apply", + logicalCloudHandler.applyHandler).Methods("POST") + // To Do + // get kubeconfig + /*lcRouter.HandleFunc( + "/logical-clouds/{name}/kubeconfig?cluster-reference={cluster}", + logicalCloudHandler.getConfigHandler).Methods("GET") + //get status + lcRouter.HandleFunc( + "/logical-clouds/{name}/cluster-references/", + logicalCloudHandler.associateHandler).Methods("GET")*/ + + // Set up Cluster API + + clusterHandler := clusterHandler{client: clusterClient} + clusterRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() + clusterRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-references", + clusterHandler.createHandler).Methods("POST") + clusterRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-references", + clusterHandler.getHandler).Methods("GET") + clusterRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", + clusterHandler.getHandler).Methods("GET") + clusterRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", + clusterHandler.updateHandler).Methods("PUT") + clusterRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", + clusterHandler.deleteHandler).Methods("DELETE") + + // Set up User Permission API + if userPermissionClient == nil { + userPermissionClient = module.NewUserPermissionClient() + } + userPermissionHandler := userPermissionHandler{client: userPermissionClient} + upRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() + upRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/user-permissions", + userPermissionHandler.createHandler).Methods("POST") + upRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}", + userPermissionHandler.getHandler).Methods("GET") + upRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}", + userPermissionHandler.updateHandler).Methods("PUT") + upRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}", + userPermissionHandler.deleteHandler).Methods("DELETE") + + // Set up Quota API + + quotaHandler := quotaHandler{client: quotaClient} + quotaRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() + quotaRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-quotas", + quotaHandler.createHandler).Methods("POST") + quotaRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}", + quotaHandler.getHandler).Methods("GET") + quotaRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}", + quotaHandler.updateHandler).Methods("PUT") + quotaRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}", + quotaHandler.deleteHandler).Methods("DELETE") + + // Set up Key Value API + if keyValueClient == nil { + keyValueClient = module.NewKeyValueClient() + } + keyValueHandler := keyValueHandler{client: keyValueClient} + kvRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter() + kvRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/kv-pairs", + keyValueHandler.createHandler).Methods("POST") + kvRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}", + keyValueHandler.getHandler).Methods("GET") + kvRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}", + keyValueHandler.updateHandler).Methods("PUT") + kvRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}", + keyValueHandler.deleteHandler).Methods("DELETE") + return router } diff --git a/src/dcm/api/clusterHandler.go b/src/dcm/api/clusterHandler.go index 3e483f06..f4a3abdc 100644 --- a/src/dcm/api/clusterHandler.go +++ b/src/dcm/api/clusterHandler.go @@ -14,149 +14,149 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "encoding/json" + "io" + "net/http" - "github.com/gorilla/mux" -) + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "github.com/gorilla/mux" +) // clusterHandler is used to store backend implementations objects type clusterHandler struct { - client module.ClusterManager + client module.ClusterManager } // CreateHandler handles creation of the cluster reference entry in the database func (h clusterHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.Cluster - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Cluster Reference Name is required. - if v.MetaData.ClusterReference == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateCluster(project, logicalCloud, v) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.Cluster + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Cluster Reference Name is required. + if v.MetaData.ClusterReference == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateCluster(project, logicalCloud, v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // getHandler handle GET operations on a particular name // Returns a Cluster Reference func (h clusterHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["cluster-reference"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllClusters(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetCluster(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["cluster-reference"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllClusters(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetCluster(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // UpdateHandler handles Update operations on a particular cluster reference func (h clusterHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.Cluster - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["cluster-reference"] - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - -// Name is required. - if v.MetaData.ClusterReference == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateCluster(project, logicalCloud, name, v) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } + var v module.Cluster + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["cluster-reference"] + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Name is required. + if v.MetaData.ClusterReference == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateCluster(project, logicalCloud, name, v) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } } //deleteHandler handles DELETE operations on a particular record func (h clusterHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["cluster-reference"] - - err := h.client.DeleteCluster(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["cluster-reference"] + + err := h.client.DeleteCluster(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/api/keyValueHandler.go b/src/dcm/api/keyValueHandler.go index 57df6556..c67504f2 100644 --- a/src/dcm/api/keyValueHandler.go +++ b/src/dcm/api/keyValueHandler.go @@ -14,148 +14,148 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" -) + "encoding/json" + "io" + "net/http" + "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" +) // keyValueHandler is used to store backend implementations objects type keyValueHandler struct { - client module.KeyValueManager + client module.KeyValueManager } // CreateHandler handles creation of the key value entry in the database func (h keyValueHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.KeyValue - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Key Value Name is required. - if v.MetaData.KeyValueName == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateKVPair(project, logicalCloud, v) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.KeyValue + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Key Value Name is required. + if v.MetaData.KeyValueName == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateKVPair(project, logicalCloud, v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // getHandler handle GET operations on a particular name // Returns a Key Value func (h keyValueHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["kv-pair-name"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllKVPairs(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetKVPair(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["kv-pair-name"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllKVPairs(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetKVPair(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // UpdateHandler handles Update operations on a particular Key Value func (h keyValueHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.KeyValue - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["kv-pair-name"] - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Name is required. - if v.MetaData.KeyValueName == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateKVPair(project, logicalCloud, name, v) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } + var v module.KeyValue + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["kv-pair-name"] + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Name is required. + if v.MetaData.KeyValueName == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateKVPair(project, logicalCloud, name, v) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } } //deleteHandler handles DELETE operations on a particular record func (h keyValueHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["kv-pair-name"] - - err := h.client.DeleteKVPair(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["kv-pair-name"] + + err := h.client.DeleteKVPair(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/api/logicalCloudHandler.go b/src/dcm/api/logicalCloudHandler.go index d8fcf268..d9a3e5f5 100644 --- a/src/dcm/api/logicalCloudHandler.go +++ b/src/dcm/api/logicalCloudHandler.go @@ -14,182 +14,181 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" -) + "encoding/json" + "io" + "net/http" + "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" +) // logicalCloudHandler is used to store backend implementations objects type logicalCloudHandler struct { - client module.LogicalCloudManager - clusterClient module.ClusterManager - quotaClient module.QuotaManager + client module.LogicalCloudManager + clusterClient module.ClusterManager + quotaClient module.QuotaManager } // CreateHandler handles creation of the logical cloud entry in the database func (h logicalCloudHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - var v module.LogicalCloud - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Logical Cloud Name is required. - if v.MetaData.LogicalCloudName == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.Create(project, v) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + var v module.LogicalCloud + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Logical Cloud Name is required. + if v.MetaData.LogicalCloudName == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.Create(project, v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // getHandler handle GET operations on a particular name // Returns a Logical Cloud func (h logicalCloudHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - name := vars["logical-cloud-name"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAll(project) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.Get(project, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + name := vars["logical-cloud-name"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAll(project) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.Get(project, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // UpdateHandler handles Update operations on a particular logical cloud func (h logicalCloudHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.LogicalCloud - vars := mux.Vars(r) - project := vars["project-name"] - name := vars["logical-cloud-name"] - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - if v.MetaData.LogicalCloudName == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.Update(project, name, v) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } + var v module.LogicalCloud + vars := mux.Vars(r) + project := vars["project-name"] + name := vars["logical-cloud-name"] + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + if v.MetaData.LogicalCloudName == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.Update(project, name, v) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } } func (h logicalCloudHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - name := vars["logical-cloud-name"] + vars := mux.Vars(r) + project := vars["project-name"] + name := vars["logical-cloud-name"] - err := h.client.Delete(project, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + err := h.client.Delete(project, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - w.WriteHeader(http.StatusNoContent) + w.WriteHeader(http.StatusNoContent) } func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - name := vars["logical-cloud-name"] - - // Get logical cloud - lc, err := h.client.Get(project, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - // Get Clusters - clusters, err := h.clusterClient.GetAllClusters(project, name) - - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - - //Get Quotas - quotas, err := h.quotaClient.GetAllQuotas(project, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - err = module.CreateEtcdContext(lc, clusters, quotas) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - return + vars := mux.Vars(r) + project := vars["project-name"] + name := vars["logical-cloud-name"] + + // Get logical cloud + lc, err := h.client.Get(project, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Get Clusters + clusters, err := h.clusterClient.GetAllClusters(project, name) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + //Get Quotas + quotas, err := h.quotaClient.GetAllQuotas(project, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + err = module.CreateEtcdContext(lc, clusters, quotas) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + return } diff --git a/src/dcm/api/quotaHandler.go b/src/dcm/api/quotaHandler.go index bca5206a..deb18e18 100644 --- a/src/dcm/api/quotaHandler.go +++ b/src/dcm/api/quotaHandler.go @@ -14,149 +14,149 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "encoding/json" + "io" + "net/http" - "github.com/gorilla/mux" -) + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "github.com/gorilla/mux" +) // quotaHandler is used to store backend implementations objects type quotaHandler struct { - client module.QuotaManager + client module.QuotaManager } // CreateHandler handles creation of the quota entry in the database func (h quotaHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.Quota - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Quota Name is required. - if v.MetaData.QuotaName == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateQuota(project, logicalCloud, v) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.Quota + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Quota Name is required. + if v.MetaData.QuotaName == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateQuota(project, logicalCloud, v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // getHandler handle GET operations on a particular name // Returns a quota func (h quotaHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["quota-name"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllQuotas(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetQuota(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["quota-name"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllQuotas(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetQuota(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // UpdateHandler handles Update operations on a particular quota func (h quotaHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.Quota - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["quota-name"] - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Name is required. - if v.MetaData.QuotaName == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateQuota(project, logicalCloud, name, v) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } + var v module.Quota + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["quota-name"] + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Name is required. + if v.MetaData.QuotaName == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateQuota(project, logicalCloud, name, v) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } } //deleteHandler handles DELETE operations on a particular record func (h quotaHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["quota-name"] - - err := h.client.DeleteQuota(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["quota-name"] + + err := h.client.DeleteQuota(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/api/userPermissionsHandler.go b/src/dcm/api/userPermissionsHandler.go index 48ab3d8e..156c390f 100644 --- a/src/dcm/api/userPermissionsHandler.go +++ b/src/dcm/api/userPermissionsHandler.go @@ -14,149 +14,149 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" -) + "encoding/json" + "io" + "net/http" + "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" +) // userPermissionHandler is used to store backend implementations objects type userPermissionHandler struct { - client module.UserPermissionManager + client module.UserPermissionManager } // CreateHandler handles creation of the user permission entry in the database func (h userPermissionHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.UserPermission - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // User-Permission Name is required. - if v.UserPermissionName == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateUserPerm(project, logicalCloud, v) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.UserPermission + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // User-Permission Name is required. + if v.UserPermissionName == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateUserPerm(project, logicalCloud, v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // getHandler handle GET operations on a particular name // Returns a User Permission func (h userPermissionHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["permission-name"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllUserPerms(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetAllUserPerms(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["permission-name"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllUserPerms(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetAllUserPerms(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } // UpdateHandler handles Update operations on a particular user permission func (h userPermissionHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.UserPermission - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["permission-name"] - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Name is required. - if v.UserPermissionName == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateUserPerm(project, logicalCloud, name, v) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - err = json.NewEncoder(w).Encode(ret) - if err != nil { - http.Error(w, err.Error(), - http.StatusInternalServerError) - return - } + var v module.UserPermission + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["permission-name"] + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Name is required. + if v.UserPermissionName == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateUserPerm(project, logicalCloud, name, v) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), + http.StatusInternalServerError) + return + } } //deleteHandler handles DELETE operations on a particular record func (h userPermissionHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["permission-name"] - - err := h.client.DeleteUserPerm(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["permission-name"] + + err := h.client.DeleteUserPerm(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/cmd/main.go b/src/dcm/cmd/main.go index 77d5348b..c08330fa 100644 --- a/src/dcm/cmd/main.go +++ b/src/dcm/cmd/main.go @@ -14,65 +14,65 @@ limitations under the License. package main import ( - "context" - "log" - "math/rand" - "net/http" - "os" - "os/signal" - "time" + "context" + "log" + "math/rand" + "net/http" + "os" + "os/signal" + "time" - "github.com/onap/multicloud-k8s/src/dcm/api" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/auth" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/config" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" - contextDb "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb" - "github.com/gorilla/handlers" + "github.com/gorilla/handlers" + "github.com/onap/multicloud-k8s/src/dcm/api" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/auth" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/config" + contextDb "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" ) func main() { - rand.Seed(time.Now().UnixNano()) + rand.Seed(time.Now().UnixNano()) - err := db.InitializeDatabaseConnection("mco") - if err != nil { - log.Println("Unable to initialize database connection...") - log.Println(err) - log.Fatalln("Exiting...") - } + err := db.InitializeDatabaseConnection("mco") + if err != nil { + log.Println("Unable to initialize database connection...") + log.Println(err) + log.Fatalln("Exiting...") + } - err = contextDb.InitializeContextDatabase() - if err != nil { - log.Println("Unable to initialize database connection...") - log.Println(err) - log.Fatalln("Exiting...") - } + err = contextDb.InitializeContextDatabase() + if err != nil { + log.Println("Unable to initialize database connection...") + log.Println(err) + log.Fatalln("Exiting...") + } - httpRouter := api.NewRouter(nil, nil, nil, nil, nil) - loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter) - log.Println("Starting Distributed Cloud Manager API") + httpRouter := api.NewRouter(nil, nil, nil, nil, nil) + loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter) + log.Println("Starting Distributed Cloud Manager API") - httpServer := &http.Server{ - Handler: loggedRouter, - Addr: ":" + config.GetConfiguration().ServicePort, - } + httpServer := &http.Server{ + Handler: loggedRouter, + Addr: ":" + config.GetConfiguration().ServicePort, + } - connectionsClose := make(chan struct{}) - go func() { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - httpServer.Shutdown(context.Background()) - close(connectionsClose) - }() + connectionsClose := make(chan struct{}) + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + httpServer.Shutdown(context.Background()) + close(connectionsClose) + }() - tlsConfig, err := auth.GetTLSConfig("ca.cert", "server.cert", "server.key") - if err != nil { - log.Println("Error Getting TLS Configuration. Starting without TLS...") - log.Fatal(httpServer.ListenAndServe()) - } else { - httpServer.TLSConfig = tlsConfig + tlsConfig, err := auth.GetTLSConfig("ca.cert", "server.cert", "server.key") + if err != nil { + log.Println("Error Getting TLS Configuration. Starting without TLS...") + log.Fatal(httpServer.ListenAndServe()) + } else { + httpServer.TLSConfig = tlsConfig - err = httpServer.ListenAndServeTLS("", "") - } + err = httpServer.ListenAndServeTLS("", "") + } } diff --git a/src/dcm/pkg/module/apply.go b/src/dcm/pkg/module/apply.go index c918f749..dbcbf8ac 100644 --- a/src/dcm/pkg/module/apply.go +++ b/src/dcm/pkg/module/apply.go @@ -1,451 +1,424 @@ -/*
-* Copyright 2020 Intel Corporation, Inc
-*
-* 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 module
-
-import (
- "strings"
- "fmt"
- "crypto/rsa"
- "crypto/rand"
- "crypto/x509"
- "crypto/x509/pkix"
- "encoding/json"
- "encoding/pem"
- "encoding/base64"
- "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext"
- log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils"
- "gopkg.in/yaml.v2"
- pkgerrors "github.com/pkg/errors"
-
-)
-
-type Resource struct {
- ApiVersion string `yaml:"apiVersion"`
- Kind string `yaml:"kind"`
- MetaData MetaDatas `yaml:"metadata"`
- Specification Specs `yaml:"spec,omitempty"`
- Rules []RoleRules `yaml:"rules,omitempty"`
- Subjects []RoleSubjects `yaml:"subjects,omitempty"`
- RoleRefs RoleRef `yaml:"roleRef,omitempty"`
-}
-
-type MetaDatas struct {
- Name string `yaml:"name"`
- Namespace string `yaml:"namespace,omitempty"`
-}
-
-type Specs struct {
- Request string `yaml:"request,omitempty"`
- Usages []string `yaml:"usages,omitempty"`
- //Hard logicalcloud.QSpec `yaml:"hard,omitempty"`
- Hard QSpec `yaml:"hard,omitempty"`
-}
-
-type RoleRules struct {
- ApiGroups []string `yaml:"apiGroups"`
- Resources []string `yaml:"resources"`
- Verbs []string `yaml:"verbs"`
-}
-
-type RoleSubjects struct {
- Kind string `yaml:"kind"`
- Name string `yaml:"name"`
- ApiGroup string `yaml:"apiGroup"`
-}
-
-type RoleRef struct {
- Kind string `yaml:"kind"`
- Name string `yaml:"name"`
- ApiGroup string `yaml:"apiGroup"`
-}
-
-
-func createNamespace(logicalcloud LogicalCloud) (string, error) {
-
- namespace := Resource{
- ApiVersion: "v1",
- Kind: "Namespace",
- MetaData: MetaDatas{
- Name: logicalcloud.Specification.NameSpace,
- },
- }
-
- nsData, err := yaml.Marshal(&namespace)
- if err != nil {
- return "", err
- }
-
-
- return string(nsData), nil
-}
-
-func createRole(logicalcloud LogicalCloud) (string, error) {
-
- userPermissions := logicalcloud.Specification.User.UserPermissions[0]
-
- role := Resource{
- ApiVersion: "rbac.authorization.k8s.io/v1beta1",
- Kind: "Role",
- MetaData: MetaDatas{
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""),
- Namespace: logicalcloud.Specification.NameSpace,
- },
- Rules: []RoleRules{ RoleRules{
- ApiGroups: userPermissions.APIGroups,
- Resources: userPermissions.Resources,
- Verbs: userPermissions.Verbs,
- },
- },
-
- }
-
- roleData, err := yaml.Marshal(&role)
- if err != nil {
- return "", err
- }
-
- return string(roleData), nil
-}
-
-func createRoleBinding(logicalcloud LogicalCloud) (string, error) {
-
- roleBinding := Resource{
- ApiVersion: "rbac.authorization.k8s.io/v1beta1",
- Kind: "RoleBinding",
- MetaData: MetaDatas{
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-roleBinding"}, ""),
- Namespace: logicalcloud.Specification.NameSpace,
- },
- Subjects: []RoleSubjects{ RoleSubjects{
- Kind: "User",
- Name: logicalcloud.Specification.User.UserName,
- ApiGroup: "",
- },
- },
-
- RoleRefs: RoleRef{
- Kind: "Role",
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""),
- ApiGroup: "",
- },
- }
-
- rBData, err := yaml.Marshal(&roleBinding)
- if err != nil {
- return "", err
- }
-
-
- return string(rBData), nil
-
-}
-
-func createQuota(quota []Quota, namespace string) (string, error) {
- lcQuota := quota[0]
-
- q := Resource{
- ApiVersion: "v1",
- Kind: "ResourceQuota",
- MetaData: MetaDatas{
- Name: lcQuota.MetaData.QuotaName,
- Namespace: namespace,
- },
- Specification: Specs{
- Hard: lcQuota.Specification,
- },
- }
-
- qData, err := yaml.Marshal(&q)
- if err != nil {
- return "", err
- }
-
-
-
- return string(qData), nil
-
-}
-
-func createUserCSR(logicalcloud LogicalCloud) (string, error) {
- KEYSIZE := 4096
- userName := logicalcloud.Specification.User.UserName
-
- key, err := rsa.GenerateKey(rand.Reader, KEYSIZE)
- if err != nil {
- return "", err
- }
-
- csrTemplate := x509.CertificateRequest{Subject: pkix.Name{CommonName: userName,},
- }
-
- csrCert, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, key)
- if err != nil {
- return "", err
- }
-
- //Encode csr
- csr := pem.EncodeToMemory(&pem.Block{
- Type: "CERTIFICATE REQUEST",
- Bytes: csrCert,
- })
-
- csrObj := Resource{
- ApiVersion: "certificates.k8s.io/v1beta1",
- Kind: "CertificateSigningRequest",
- MetaData: MetaDatas{
- Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-user-csr"}, ""),
- Namespace: logicalcloud.Specification.NameSpace,
- },
- Specification: Specs{
- Request: base64.StdEncoding.EncodeToString(csr),
- Usages: []string{"digital signature", "key encipherment"},
- },
- }
-
- csrData, err := yaml.Marshal(&csrObj)
- if err != nil {
- return "", err
- }
-
-
- return string(csrData), nil
-
-
-}
-
-// TODO:
-// Install istio
-// Store user key for user creation
-// Code to run kubectl commands for user
-// kubectl certificate approve lc1-user-cert
-// kubectl get csr lc1-user-cert -o jsonpath='{.status.certificate}' | base64 --decode > user.crt
-// kubectl config set-credentials user --client-certificate=<user.crt> --client-key=<user.key>
-// kubectl config set-context user-context --cluster=cluster-name --namespace=lc1 --user=user
-
-
-func CreateEtcdContext(logicalcloud LogicalCloud, clusterList []Cluster,
- quotaList []Quota ) error {
-
- APP := "logical-cloud"
- logicalCloudName := logicalcloud.MetaData.LogicalCloudName
-
-
- //Resource Names
- namespaceName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+namespace"}, "")
- roleName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+role"}, "")
- roleBindingName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+roleBinding"}, "")
- quotaName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+quota"}, "")
- csrName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+CertificateSigningRequest"}, "")
-
- // Get resources to be added
- namespace, err := createNamespace(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating Namespace YAML for logical cloud")
- }
-
- role, err := createRole(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating Role YAML for logical cloud")
- }
-
- roleBinding, err := createRoleBinding(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating RoleBinding YAML for logical cloud")
- }
-
- quota, err := createQuota(quotaList, logicalcloud.Specification.NameSpace)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating Quota YAML for logical cloud")
- }
-
- csr, err := createUserCSR(logicalcloud)
- if err != nil {
- return pkgerrors.Wrap(err, "Error Creating User CSR for logical cloud")
- }
-
-
- context := appcontext.AppContext{}
- ctxVal, err := context.InitAppContext()
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating AppContext")
- }
-
- fmt.Printf("%v\n", ctxVal)
-
- handle, err := context.CreateCompositeApp()
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating AppContext CompositeApp")
- }
-
-
- appHandle, err := context.AddApp(handle, APP)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext CompositeApp create failure", log.Fields{
- "logical-cloud": logicalCloudName,
- })
- }
- return pkgerrors.Wrap(err, "Error adding App to AppContext")
- }
-
-
- // Iterate through cluster list and add all the clusters
- for _, cluster:= range clusterList {
- clusterName := strings.Join([]string{cluster.Specification.ClusterProvider, "+", cluster.Specification.ClusterName, }, "")
- clusterHandle, err := context.AddCluster(appHandle, clusterName)
-
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add cluster failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding Cluster to AppContext")
- }
-
- // Add namespace resource to each cluster
- _, err = context.AddResource(clusterHandle, namespaceName, namespace)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add namespace resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding Namespace Resource to AppContext")
- }
-
- // Add csr resource to each cluster
- _, err = context.AddResource(clusterHandle, csrName, csr)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add CSR resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding CSR Resource to AppContext")
- }
-
-
-
- // Add Role resource to each cluster
- _, err = context.AddResource(clusterHandle, roleName, role)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add role resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding role Resource to AppContext")
- }
-
- // Add RoleBinding resource to each cluster
- _, err = context.AddResource(clusterHandle, roleBindingName, roleBinding)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add roleBinding resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding roleBinding Resource to AppContext")
- }
-
- // Add quota resource to each cluster
- _, err = context.AddResource(clusterHandle, quotaName, quota)
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add quota resource failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding quota Resource to AppContext")
- }
-
- // Add Resource Order and Resource Dependency
- resOrder, err := json.Marshal(map[string][]string{"resorder" : []string{namespaceName, quotaName, csrName, roleName, roleBindingName}})
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating resource order JSON")
- }
-
- resDependency, err := json.Marshal(map[string]map[string]string{"resdependency" : map[string]string{namespaceName : "go",
- quotaName : strings.Join([]string{"wait on ", namespaceName}, ""), csrName: strings.Join([]string{"wait on ", quotaName}, ""),
- roleName : strings.Join([]string{"wait on ", csrName}, ""), roleBindingName: strings.Join([]string{"wait on ", roleName}, ""),}})
-
- if err != nil {
- return pkgerrors.Wrap(err, "Error creating resource dependency JSON")
- }
-
- _, err = context.AddInstruction(clusterHandle, "resource", "order", string(resOrder))
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding instruction order to AppContext")
- }
-
- _, err = context.AddInstruction(clusterHandle, "resource", "dependency", string(resDependency))
- if err != nil {
- cleanuperr := context.DeleteCompositeApp()
- if cleanuperr != nil {
- log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{
- "cluster-provider": cluster.Specification.ClusterProvider,
- "cluster": cluster.Specification.ClusterName,
- "logical-cloud": logicalCloudName,
-
- })
- }
- return pkgerrors.Wrap(err, "Error adding instruction dependency to AppContext")
- }
-
-
-
- }
-
- return nil
-
-
-}
+/* +* Copyright 2020 Intel Corporation, Inc +* +* 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 module + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "crypto/x509/pkix" + "encoding/base64" + "encoding/json" + "encoding/pem" + "fmt" + "strings" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" + log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" + pkgerrors "github.com/pkg/errors" + "gopkg.in/yaml.v2" +) + +type Resource struct { + ApiVersion string `yaml:"apiVersion"` + Kind string `yaml:"kind"` + MetaData MetaDatas `yaml:"metadata"` + Specification Specs `yaml:"spec,omitempty"` + Rules []RoleRules `yaml:"rules,omitempty"` + Subjects []RoleSubjects `yaml:"subjects,omitempty"` + RoleRefs RoleRef `yaml:"roleRef,omitempty"` +} + +type MetaDatas struct { + Name string `yaml:"name"` + Namespace string `yaml:"namespace,omitempty"` +} + +type Specs struct { + Request string `yaml:"request,omitempty"` + Usages []string `yaml:"usages,omitempty"` + //Hard logicalcloud.QSpec `yaml:"hard,omitempty"` + Hard QSpec `yaml:"hard,omitempty"` +} + +type RoleRules struct { + ApiGroups []string `yaml:"apiGroups"` + Resources []string `yaml:"resources"` + Verbs []string `yaml:"verbs"` +} + +type RoleSubjects struct { + Kind string `yaml:"kind"` + Name string `yaml:"name"` + ApiGroup string `yaml:"apiGroup"` +} + +type RoleRef struct { + Kind string `yaml:"kind"` + Name string `yaml:"name"` + ApiGroup string `yaml:"apiGroup"` +} + +func createNamespace(logicalcloud LogicalCloud) (string, error) { + + namespace := Resource{ + ApiVersion: "v1", + Kind: "Namespace", + MetaData: MetaDatas{ + Name: logicalcloud.Specification.NameSpace, + }, + } + + nsData, err := yaml.Marshal(&namespace) + if err != nil { + return "", err + } + + return string(nsData), nil +} + +func createRole(logicalcloud LogicalCloud) (string, error) { + + userPermissions := logicalcloud.Specification.User.UserPermissions[0] + + role := Resource{ + ApiVersion: "rbac.authorization.k8s.io/v1beta1", + Kind: "Role", + MetaData: MetaDatas{ + Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""), + Namespace: logicalcloud.Specification.NameSpace, + }, + Rules: []RoleRules{RoleRules{ + ApiGroups: userPermissions.APIGroups, + Resources: userPermissions.Resources, + Verbs: userPermissions.Verbs, + }, + }, + } + + roleData, err := yaml.Marshal(&role) + if err != nil { + return "", err + } + + return string(roleData), nil +} + +func createRoleBinding(logicalcloud LogicalCloud) (string, error) { + + roleBinding := Resource{ + ApiVersion: "rbac.authorization.k8s.io/v1beta1", + Kind: "RoleBinding", + MetaData: MetaDatas{ + Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-roleBinding"}, ""), + Namespace: logicalcloud.Specification.NameSpace, + }, + Subjects: []RoleSubjects{RoleSubjects{ + Kind: "User", + Name: logicalcloud.Specification.User.UserName, + ApiGroup: "", + }, + }, + + RoleRefs: RoleRef{ + Kind: "Role", + Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-role"}, ""), + ApiGroup: "", + }, + } + + rBData, err := yaml.Marshal(&roleBinding) + if err != nil { + return "", err + } + + return string(rBData), nil + +} + +func createQuota(quota []Quota, namespace string) (string, error) { + lcQuota := quota[0] + + q := Resource{ + ApiVersion: "v1", + Kind: "ResourceQuota", + MetaData: MetaDatas{ + Name: lcQuota.MetaData.QuotaName, + Namespace: namespace, + }, + Specification: Specs{ + Hard: lcQuota.Specification, + }, + } + + qData, err := yaml.Marshal(&q) + if err != nil { + return "", err + } + + return string(qData), nil + +} + +func createUserCSR(logicalcloud LogicalCloud) (string, error) { + KEYSIZE := 4096 + userName := logicalcloud.Specification.User.UserName + + key, err := rsa.GenerateKey(rand.Reader, KEYSIZE) + if err != nil { + return "", err + } + + csrTemplate := x509.CertificateRequest{Subject: pkix.Name{CommonName: userName}} + + csrCert, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, key) + if err != nil { + return "", err + } + + //Encode csr + csr := pem.EncodeToMemory(&pem.Block{ + Type: "CERTIFICATE REQUEST", + Bytes: csrCert, + }) + + csrObj := Resource{ + ApiVersion: "certificates.k8s.io/v1beta1", + Kind: "CertificateSigningRequest", + MetaData: MetaDatas{ + Name: strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "-user-csr"}, ""), + Namespace: logicalcloud.Specification.NameSpace, + }, + Specification: Specs{ + Request: base64.StdEncoding.EncodeToString(csr), + Usages: []string{"digital signature", "key encipherment"}, + }, + } + + csrData, err := yaml.Marshal(&csrObj) + if err != nil { + return "", err + } + + return string(csrData), nil + +} + +// TODO: +// Install istio +// Store user key for user creation +// Code to run kubectl commands for user +// kubectl certificate approve lc1-user-cert +// kubectl get csr lc1-user-cert -o jsonpath='{.status.certificate}' | base64 --decode > user.crt +// kubectl config set-credentials user --client-certificate=<user.crt> --client-key=<user.key> +// kubectl config set-context user-context --cluster=cluster-name --namespace=lc1 --user=user + +func CreateEtcdContext(logicalcloud LogicalCloud, clusterList []Cluster, + quotaList []Quota) error { + + APP := "logical-cloud" + logicalCloudName := logicalcloud.MetaData.LogicalCloudName + + //Resource Names + namespaceName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+namespace"}, "") + roleName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+role"}, "") + roleBindingName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+roleBinding"}, "") + quotaName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+quota"}, "") + csrName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+CertificateSigningRequest"}, "") + + // Get resources to be added + namespace, err := createNamespace(logicalcloud) + if err != nil { + return pkgerrors.Wrap(err, "Error Creating Namespace YAML for logical cloud") + } + + role, err := createRole(logicalcloud) + if err != nil { + return pkgerrors.Wrap(err, "Error Creating Role YAML for logical cloud") + } + + roleBinding, err := createRoleBinding(logicalcloud) + if err != nil { + return pkgerrors.Wrap(err, "Error Creating RoleBinding YAML for logical cloud") + } + + quota, err := createQuota(quotaList, logicalcloud.Specification.NameSpace) + if err != nil { + return pkgerrors.Wrap(err, "Error Creating Quota YAML for logical cloud") + } + + csr, err := createUserCSR(logicalcloud) + if err != nil { + return pkgerrors.Wrap(err, "Error Creating User CSR for logical cloud") + } + + context := appcontext.AppContext{} + ctxVal, err := context.InitAppContext() + if err != nil { + return pkgerrors.Wrap(err, "Error creating AppContext") + } + + fmt.Printf("%v\n", ctxVal) + + handle, err := context.CreateCompositeApp() + if err != nil { + return pkgerrors.Wrap(err, "Error creating AppContext CompositeApp") + } + + appHandle, err := context.AddApp(handle, APP) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext CompositeApp create failure", log.Fields{ + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding App to AppContext") + } + + // Iterate through cluster list and add all the clusters + for _, cluster := range clusterList { + clusterName := strings.Join([]string{cluster.Specification.ClusterProvider, "+", cluster.Specification.ClusterName}, "") + clusterHandle, err := context.AddCluster(appHandle, clusterName) + + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add cluster failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding Cluster to AppContext") + } + + // Add namespace resource to each cluster + _, err = context.AddResource(clusterHandle, namespaceName, namespace) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add namespace resource failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding Namespace Resource to AppContext") + } + + // Add csr resource to each cluster + _, err = context.AddResource(clusterHandle, csrName, csr) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add CSR resource failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding CSR Resource to AppContext") + } + + // Add Role resource to each cluster + _, err = context.AddResource(clusterHandle, roleName, role) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add role resource failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding role Resource to AppContext") + } + + // Add RoleBinding resource to each cluster + _, err = context.AddResource(clusterHandle, roleBindingName, roleBinding) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add roleBinding resource failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding roleBinding Resource to AppContext") + } + + // Add quota resource to each cluster + _, err = context.AddResource(clusterHandle, quotaName, quota) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add quota resource failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding quota Resource to AppContext") + } + + // Add Resource Order and Resource Dependency + resOrder, err := json.Marshal(map[string][]string{"resorder": []string{namespaceName, quotaName, csrName, roleName, roleBindingName}}) + if err != nil { + return pkgerrors.Wrap(err, "Error creating resource order JSON") + } + + resDependency, err := json.Marshal(map[string]map[string]string{"resdependency": map[string]string{namespaceName: "go", + quotaName: strings.Join([]string{"wait on ", namespaceName}, ""), csrName: strings.Join([]string{"wait on ", quotaName}, ""), + roleName: strings.Join([]string{"wait on ", csrName}, ""), roleBindingName: strings.Join([]string{"wait on ", roleName}, "")}}) + + if err != nil { + return pkgerrors.Wrap(err, "Error creating resource dependency JSON") + } + + _, err = context.AddInstruction(clusterHandle, "resource", "order", string(resOrder)) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding instruction order to AppContext") + } + + _, err = context.AddInstruction(clusterHandle, "resource", "dependency", string(resDependency)) + if err != nil { + cleanuperr := context.DeleteCompositeApp() + if cleanuperr != nil { + log.Warn("Error cleaning AppContext after add instruction failure", log.Fields{ + "cluster-provider": cluster.Specification.ClusterProvider, + "cluster": cluster.Specification.ClusterName, + "logical-cloud": logicalCloudName, + }) + } + return pkgerrors.Wrap(err, "Error adding instruction dependency to AppContext") + } + + } + + return nil + +} diff --git a/src/dcm/pkg/module/cluster.go b/src/dcm/pkg/module/cluster.go index 38848990..206d79a6 100644 --- a/src/dcm/pkg/module/cluster.go +++ b/src/dcm/pkg/module/cluster.go @@ -12,194 +12,192 @@ * 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 module import ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) // Cluster contains the parameters needed for a Cluster type Cluster struct { - MetaData ClusterMeta `json:"metadata"` - Specification ClusterSpec `json:"spec"` + MetaData ClusterMeta `json:"metadata"` + Specification ClusterSpec `json:"spec"` } type ClusterMeta struct { - ClusterReference string `json:"name"` - Description string `json:"description"` - UserData1 string `json:"userData1"` - UserData2 string `json:"userData2"` + ClusterReference string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` } type ClusterSpec struct { - ClusterProvider string `json:"cluster-provider"` - ClusterName string `json:"cluster-name"` - LoadBalancerIP string `json:"loadbalancer-ip"` + ClusterProvider string `json:"cluster-provider"` + ClusterName string `json:"cluster-name"` + LoadBalancerIP string `json:"loadbalancer-ip"` } - type ClusterKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - ClusterReference string `json:"clname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + ClusterReference string `json:"clname"` } // ClusterManager is an interface that exposes the connection // functionality type ClusterManager interface { - CreateCluster(project, logicalCloud string, c Cluster) (Cluster, error) - GetCluster(project, logicalCloud, name string) (Cluster, error) - GetAllClusters(project, logicalCloud string) ([]Cluster, error) - DeleteCluster(project, logicalCloud, name string) error - UpdateCluster(project, logicalCloud, name string, c Cluster) (Cluster, error) + CreateCluster(project, logicalCloud string, c Cluster) (Cluster, error) + GetCluster(project, logicalCloud, name string) (Cluster, error) + GetAllClusters(project, logicalCloud string) ([]Cluster, error) + DeleteCluster(project, logicalCloud, name string) error + UpdateCluster(project, logicalCloud, name string, c Cluster) (Cluster, error) } // ClusterClient implements the ClusterManager // It will also be used to maintain some localized state type ClusterClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // ClusterClient returns an instance of the ClusterClient // which implements the ClusterManager func NewClusterClient() *ClusterClient { - service := DBService{} - return &ClusterClient{ - storeName: "orchestrator", - tagMeta: "cluster", - util: service, - } + service := DBService{} + return &ClusterClient{ + storeName: "orchestrator", + tagMeta: "cluster", + util: service, + } } // Create entry for the cluster reference resource in the database func (v *ClusterClient) CreateCluster(project, logicalCloud string, c Cluster) (Cluster, error) { - //Construct key consisting of name - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: c.MetaData.ClusterReference, - } - - //Check if project exists - err := v.util.CheckProject(project) - if err != nil { - return Cluster{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return Cluster{}, pkgerrors.New("Unable to find the logical cloud") - } - //Check if this Cluster reference already exists - _, err = v.GetCluster(project, logicalCloud, c.MetaData.ClusterReference) - if err == nil { - return Cluster{}, pkgerrors.New("Cluster reference already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: c.MetaData.ClusterReference, + } + + //Check if project exists + err := v.util.CheckProject(project) + if err != nil { + return Cluster{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return Cluster{}, pkgerrors.New("Unable to find the logical cloud") + } + //Check if this Cluster reference already exists + _, err = v.GetCluster(project, logicalCloud, c.MetaData.ClusterReference) + if err == nil { + return Cluster{}, pkgerrors.New("Cluster reference already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns Cluster for corresponding cluster reference -func (v *ClusterClient) GetCluster(project, logicalCloud, clusterReference string)(Cluster, error) { - - //Construct the composite key to select the entry - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: clusterReference, - } - - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Get Cluster reference") - } - - //value is a byte array - if value != nil { - cl := Cluster{} - err = v.util.DBUnmarshal(value[0], &cl) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return cl, nil - } - - return Cluster{}, pkgerrors.New("Error getting Cluster") +func (v *ClusterClient) GetCluster(project, logicalCloud, clusterReference string) (Cluster, error) { + + //Construct the composite key to select the entry + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: clusterReference, + } + + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Get Cluster reference") + } + + //value is a byte array + if value != nil { + cl := Cluster{} + err = v.util.DBUnmarshal(value[0], &cl) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return cl, nil + } + + return Cluster{}, pkgerrors.New("Error getting Cluster") } - // GetAll returns all cluster references in the logical cloud -func (v *ClusterClient) GetAllClusters(project, logicalCloud string)([]Cluster, error) { - //Construct the composite key to select clusters - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: "", - } - var resp []Cluster - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []Cluster{}, pkgerrors.Wrap(err, "Get All Cluster references") - } - - for _, value := range values { - cl := Cluster{} - err = v.util.DBUnmarshal(value, &cl) - if err != nil { - return []Cluster{}, pkgerrors.Wrap(err, "Unmarshaling values") - } - resp = append(resp, cl) - } - - return resp, nil +func (v *ClusterClient) GetAllClusters(project, logicalCloud string) ([]Cluster, error) { + //Construct the composite key to select clusters + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: "", + } + var resp []Cluster + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []Cluster{}, pkgerrors.Wrap(err, "Get All Cluster references") + } + + for _, value := range values { + cl := Cluster{} + err = v.util.DBUnmarshal(value, &cl) + if err != nil { + return []Cluster{}, pkgerrors.Wrap(err, "Unmarshaling values") + } + resp = append(resp, cl) + } + + return resp, nil } // Delete the Cluster reference entry from database func (v *ClusterClient) DeleteCluster(project, logicalCloud, clusterReference string) error { - //Construct the composite key to select the entry - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: clusterReference, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Cluster Reference") - } - return nil + //Construct the composite key to select the entry + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: clusterReference, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Cluster Reference") + } + return nil } // Update an entry for the Cluster reference in the database func (v *ClusterClient) UpdateCluster(project, logicalCloud, clusterReference string, c Cluster) (Cluster, error) { - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: clusterReference, - } - - //Check for name mismatch in cluster reference - if c.MetaData.ClusterReference != clusterReference { - return Cluster{}, pkgerrors.New("Update Error - Cluster reference mismatch") - } - //Check if this Cluster reference exists - _, err := v.GetCluster(project, logicalCloud, clusterReference) - if err != nil { - return Cluster{}, pkgerrors.New("Update Error - Cluster reference doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: clusterReference, + } + + //Check for name mismatch in cluster reference + if c.MetaData.ClusterReference != clusterReference { + return Cluster{}, pkgerrors.New("Update Error - Cluster reference mismatch") + } + //Check if this Cluster reference exists + _, err := v.GetCluster(project, logicalCloud, clusterReference) + if err != nil { + return Cluster{}, pkgerrors.New("Update Error - Cluster reference doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/cluster_test.go b/src/dcm/pkg/module/cluster_test.go index d42935db..626adff7 100644 --- a/src/dcm/pkg/module/cluster_test.go +++ b/src/dcm/pkg/module/cluster_test.go @@ -1,115 +1,112 @@ -package module
-
-import (
- "testing"
-
- "github.com/pkg/errors"
-
-)
-
-
-func TestCreateCluster(t *testing.T) {
-
- mData := ClusterMeta{
- ClusterReference: "test_cluster",
- }
-
- cl := Cluster {
- MetaData: mData,
- }
- data1 := [][]byte{}
-
-
- key := ClusterKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- ClusterReference: "test_cluster",
- }
- myMocks := new(mockValues)
- // just to get an error value
- err1 := errors.New("math: square root of negative number")
-
- myMocks.On("CheckProject", "test_project").Return(nil)
- myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil)
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1)
-
- clClient := ClusterClient{"test_dcm", "test_meta", myMocks}
- _, err := clClient.CreateCluster("test_project", "test_asdf", cl)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestGetCluster(t *testing.T) {
- key := ClusterKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- ClusterReference: "test_cluster",
- }
-
- data1 := [][]byte{
- []byte("abc"),
- }
-
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- clClient := ClusterClient{"test_dcm", "test_meta", myMocks}
- _, err := clClient.GetCluster("test_project", "test_asdf", "test_cluster")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestDeleteCluster(t *testing.T) {
-
- key := ClusterKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- ClusterReference: "test_cluster",
- }
-
- myMocks := new(mockValues)
-
- myMocks.On("DBRemove", "test_dcm", key).Return(nil)
-
- clClient := ClusterClient{"test_dcm", "test_meta", myMocks}
- err := clClient.DeleteCluster("test_project", "test_asdf", "test_cluster")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-
-}
-
-func TestUpdateCluster(t *testing.T) {
- key := ClusterKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- ClusterReference: "test_cluster",
- }
- mData := ClusterMeta{
- ClusterReference: "test_cluster",
- }
- cl := Cluster{
- MetaData: mData,
- }
- data1 := [][]byte{
- []byte("abc"),
- }
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- clClient := ClusterClient{"test_dcm", "test_meta", myMocks}
- _, err := clClient.UpdateCluster("test_project", "test_asdf", "test_cluster", cl)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
+package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateCluster(t *testing.T) { + + mData := ClusterMeta{ + ClusterReference: "test_cluster", + } + + cl := Cluster{ + MetaData: mData, + } + data1 := [][]byte{} + + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + _, err := clClient.CreateCluster("test_project", "test_asdf", cl) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetCluster(t *testing.T) { + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + _, err := clClient.GetCluster("test_project", "test_asdf", "test_cluster") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteCluster(t *testing.T) { + + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + err := clClient.DeleteCluster("test_project", "test_asdf", "test_cluster") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateCluster(t *testing.T) { + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + mData := ClusterMeta{ + ClusterReference: "test_cluster", + } + cl := Cluster{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + _, err := clClient.UpdateCluster("test_project", "test_asdf", "test_cluster", cl) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/keyvalue.go b/src/dcm/pkg/module/keyvalue.go index 4e3e0fab..37c74a84 100644 --- a/src/dcm/pkg/module/keyvalue.go +++ b/src/dcm/pkg/module/keyvalue.go @@ -12,194 +12,193 @@ * 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 module import ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) // KeyValue contains the parameters needed for a key value type KeyValue struct { - MetaData KVMetaDataList `json:"metadata"` - Specification KVSpec `json:"spec"` + MetaData KVMetaDataList `json:"metadata"` + Specification KVSpec `json:"spec"` } - // MetaData contains the parameters needed for metadata type KVMetaDataList struct { - KeyValueName string `json:"name"` - Description string `json:"description"` - UserData1 string `json:"userData1"` - UserData2 string `json:"userData2"` + KeyValueName string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` } // Spec contains the parameters needed for spec type KVSpec struct { - Kv []map[string]interface{} `json:"kv"` + Kv []map[string]interface{} `json:"kv"` } // KeyValueKey is the key structure that is used in the database type KeyValueKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - KeyValueName string `json:"kvname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + KeyValueName string `json:"kvname"` } // KeyValueManager is an interface that exposes the connection // functionality type KeyValueManager interface { - CreateKVPair(project, logicalCloud string, c KeyValue) (KeyValue, error) - GetKVPair(project, logicalCloud, name string) (KeyValue, error) - GetAllKVPairs(project, logicalCloud string) ([]KeyValue, error) - DeleteKVPair(project, logicalCloud, name string) error - UpdateKVPair(project, logicalCloud, name string, c KeyValue) (KeyValue, error) + CreateKVPair(project, logicalCloud string, c KeyValue) (KeyValue, error) + GetKVPair(project, logicalCloud, name string) (KeyValue, error) + GetAllKVPairs(project, logicalCloud string) ([]KeyValue, error) + DeleteKVPair(project, logicalCloud, name string) error + UpdateKVPair(project, logicalCloud, name string, c KeyValue) (KeyValue, error) } // KeyValueClient implements the KeyValueManager // It will also be used to maintain some localized state type KeyValueClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // KeyValueClient returns an instance of the KeyValueClient // which implements the KeyValueManager func NewKeyValueClient() *KeyValueClient { - service := DBService{} - return &KeyValueClient{ - storeName: "orchestrator", - tagMeta: "keyvalue", - util: service, - } + service := DBService{} + return &KeyValueClient{ + storeName: "orchestrator", + tagMeta: "keyvalue", + util: service, + } } // Create entry for the key value resource in the database func (v *KeyValueClient) CreateKVPair(project, logicalCloud string, c KeyValue) (KeyValue, error) { - //Construct key consisting of name - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: c.MetaData.KeyValueName, - } - - //Check if project exist - err := v.util.CheckProject(project) - if err != nil { - return KeyValue{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return KeyValue{}, pkgerrors.New("Unable to find the logical cloud") - } - //Check if this Key Value already exists - _, err = v.GetKVPair(project, logicalCloud, c.MetaData.KeyValueName) - if err == nil { - return KeyValue{}, pkgerrors.New("Key Value already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: c.MetaData.KeyValueName, + } + + //Check if project exist + err := v.util.CheckProject(project) + if err != nil { + return KeyValue{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return KeyValue{}, pkgerrors.New("Unable to find the logical cloud") + } + //Check if this Key Value already exists + _, err = v.GetKVPair(project, logicalCloud, c.MetaData.KeyValueName) + if err == nil { + return KeyValue{}, pkgerrors.New("Key Value already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns Key Value for correspondin name func (v *KeyValueClient) GetKVPair(project, logicalCloud, kvPairName string) (KeyValue, error) { - //Construct the composite key to select the entry - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: kvPairName, - } - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") - } - - //value is a byte array - if value != nil { - kv := KeyValue{} - err = v.util.DBUnmarshal(value[0], &kv) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return kv, nil - } - - return KeyValue{}, pkgerrors.New("Error getting Key Value") + //Construct the composite key to select the entry + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: kvPairName, + } + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") + } + + //value is a byte array + if value != nil { + kv := KeyValue{} + err = v.util.DBUnmarshal(value[0], &kv) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return kv, nil + } + + return KeyValue{}, pkgerrors.New("Error getting Key Value") } // Get All lists all key value pairs func (v *KeyValueClient) GetAllKVPairs(project, logicalCloud string) ([]KeyValue, error) { - //Construct the composite key to select the entry - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: "", - } - var resp []KeyValue - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") - } - - for _, value := range values { - kv := KeyValue{} - err = v.util.DBUnmarshal(value, &kv) - if err != nil { - return []KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - resp = append(resp, kv) - } - - return resp, nil + //Construct the composite key to select the entry + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: "", + } + var resp []KeyValue + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") + } + + for _, value := range values { + kv := KeyValue{} + err = v.util.DBUnmarshal(value, &kv) + if err != nil { + return []KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + resp = append(resp, kv) + } + + return resp, nil } // Delete the Key Value entry from database func (v *KeyValueClient) DeleteKVPair(project, logicalCloud, kvPairName string) error { - //Construct the composite key to select the entry - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: kvPairName, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Key Value") - } - return nil + //Construct the composite key to select the entry + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: kvPairName, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Key Value") + } + return nil } // Update an entry for the Key Value in the database func (v *KeyValueClient) UpdateKVPair(project, logicalCloud, kvPairName string, c KeyValue) (KeyValue, error) { - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: kvPairName, - } - //Check if KV pair URl name is the same name in json - if c.MetaData.KeyValueName != kvPairName { - return KeyValue{}, pkgerrors.New("Update Error - KV pair name mismatch") - } - //Check if this Key Value exists - _, err := v.GetKVPair(project, logicalCloud, kvPairName) - if err != nil { - return KeyValue{}, pkgerrors.New("Update Error - Key Value Pair doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: kvPairName, + } + //Check if KV pair URl name is the same name in json + if c.MetaData.KeyValueName != kvPairName { + return KeyValue{}, pkgerrors.New("Update Error - KV pair name mismatch") + } + //Check if this Key Value exists + _, err := v.GetKVPair(project, logicalCloud, kvPairName) + if err != nil { + return KeyValue{}, pkgerrors.New("Update Error - Key Value Pair doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/keyvalue_test.go b/src/dcm/pkg/module/keyvalue_test.go index 9faceda4..25fd55cb 100644 --- a/src/dcm/pkg/module/keyvalue_test.go +++ b/src/dcm/pkg/module/keyvalue_test.go @@ -1,115 +1,112 @@ -package module
-
-import (
- "testing"
-
- "github.com/pkg/errors"
-
-)
-
-
-func TestCreateKVPair(t *testing.T) {
-
- mData := KVMetaDataList{
- KeyValueName: "test_kv_pair",
- }
-
- kv := KeyValue {
- MetaData: mData,
- }
- data1 := [][]byte{}
-
-
- key := KeyValueKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- KeyValueName: "test_kv_pair",
- }
- myMocks := new(mockValues)
- // just to get an error value
- err1 := errors.New("math: square root of negative number")
-
- myMocks.On("CheckProject", "test_project").Return(nil)
- myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil)
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1)
-
- kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks}
- _, err := kvClient.CreateKVPair("test_project", "test_asdf", kv)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestGetKVPair(t *testing.T) {
- key := KeyValueKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- KeyValueName: "test_kv_pair",
- }
-
- data1 := [][]byte{
- []byte("abc"),
- }
-
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks}
- _, err := kvClient.GetKVPair("test_project", "test_asdf", "test_kv_pair")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestDeleteKVPair(t *testing.T) {
-
- key := KeyValueKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- KeyValueName: "test_kv_pair",
- }
-
- myMocks := new(mockValues)
-
- myMocks.On("DBRemove", "test_dcm", key).Return(nil)
-
- kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks}
- err := kvClient.DeleteKVPair("test_project", "test_asdf", "test_kv_pair")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-
-}
-
-func TestUpdateKVPair(t *testing.T) {
- key := KeyValueKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- KeyValueName: "test_kv_pair",
- }
- mData := KVMetaDataList{
- KeyValueName: "test_kv_pair",
- }
- kv := KeyValue{
- MetaData: mData,
- }
- data1 := [][]byte{
- []byte("abc"),
- }
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks}
- _, err := kvClient.UpdateKVPair("test_project", "test_asdf", "test_kv_pair", kv)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
+package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateKVPair(t *testing.T) { + + mData := KVMetaDataList{ + KeyValueName: "test_kv_pair", + } + + kv := KeyValue{ + MetaData: mData, + } + data1 := [][]byte{} + + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + _, err := kvClient.CreateKVPair("test_project", "test_asdf", kv) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetKVPair(t *testing.T) { + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + _, err := kvClient.GetKVPair("test_project", "test_asdf", "test_kv_pair") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteKVPair(t *testing.T) { + + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + err := kvClient.DeleteKVPair("test_project", "test_asdf", "test_kv_pair") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateKVPair(t *testing.T) { + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + mData := KVMetaDataList{ + KeyValueName: "test_kv_pair", + } + kv := KeyValue{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + _, err := kvClient.UpdateKVPair("test_project", "test_asdf", "test_kv_pair", kv) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/logicalcloud.go b/src/dcm/pkg/module/logicalcloud.go index 9fb1b6fb..51ee387d 100644 --- a/src/dcm/pkg/module/logicalcloud.go +++ b/src/dcm/pkg/module/logicalcloud.go @@ -12,280 +12,277 @@ * 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 module import ( - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) // LogicalCloud contains the parameters needed for a Logical Cloud type LogicalCloud struct { - MetaData MetaDataList `json:"metadata"` - Specification Spec `json:"spec"` + MetaData MetaDataList `json:"metadata"` + Specification Spec `json:"spec"` } - // MetaData contains the parameters needed for metadata type MetaDataList struct { - LogicalCloudName string `json:"name"` - Description string `json:"description"` - UserData1 string `json:"userData1"` - UserData2 string `json:"userData2"` + LogicalCloudName string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` } // Spec contains the parameters needed for spec type Spec struct { - NameSpace string `json:"namespace"` - User UserData `json:"user"` - + NameSpace string `json:"namespace"` + User UserData `json:"user"` } // UserData contains the parameters needed for user type UserData struct { - UserName string `json:"user-name"` - Type string `json:"type"` - UserPermissions []UserPerm `json:"user-permissions"` + UserName string `json:"user-name"` + Type string `json:"type"` + UserPermissions []UserPerm `json:"user-permissions"` } // UserPerm contains the parameters needed for user permissions type UserPerm struct { - PermName string `json:"permission-name"` - APIGroups []string `json:"apiGroups"` - Resources []string `json:"resources"` - Verbs []string `json:"verbs"` + PermName string `json:"permission-name"` + APIGroups []string `json:"apiGroups"` + Resources []string `json:"resources"` + Verbs []string `json:"verbs"` } // LogicalCloudKey is the key structure that is used in the database type LogicalCloudKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` } // LogicalCloudManager is an interface that exposes the connection // functionality type LogicalCloudManager interface { - Create(project string, c LogicalCloud) (LogicalCloud, error) - Get(project, name string) (LogicalCloud, error) - GetAll(project string) ([]LogicalCloud, error) - Delete(project, name string) error - Update(project, name string, c LogicalCloud) (LogicalCloud, error) - + Create(project string, c LogicalCloud) (LogicalCloud, error) + Get(project, name string) (LogicalCloud, error) + GetAll(project string) ([]LogicalCloud, error) + Delete(project, name string) error + Update(project, name string, c LogicalCloud) (LogicalCloud, error) } // Interface facilitates unit testing by mocking functions type Utility interface { - DBInsert(storeName string, key db.Key, query interface{}, meta string, c interface{}) error - DBFind(storeName string, key db.Key, meta string) ([][]byte, error) - DBUnmarshal(value []byte, out interface{}) error - DBRemove(storeName string, key db.Key) error - CheckProject(project string) error - CheckLogicalCloud(project, logicalCloud string) error + DBInsert(storeName string, key db.Key, query interface{}, meta string, c interface{}) error + DBFind(storeName string, key db.Key, meta string) ([][]byte, error) + DBUnmarshal(value []byte, out interface{}) error + DBRemove(storeName string, key db.Key) error + CheckProject(project string) error + CheckLogicalCloud(project, logicalCloud string) error } // LogicalCloudClient implements the LogicalCloudManager // It will also be used to maintain some localized state type LogicalCloudClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // Added for unit testing; implements Utility interface -type DBService struct {} +type DBService struct{} // LogicalCloudClient returns an instance of the LogicalCloudClient // which implements the LogicalCloudManager func NewLogicalCloudClient() *LogicalCloudClient { - service := DBService{} - return &LogicalCloudClient{ - storeName: "orchestrator", - tagMeta: "logicalcloud", - util: service, - } + service := DBService{} + return &LogicalCloudClient{ + storeName: "orchestrator", + tagMeta: "logicalcloud", + util: service, + } } // Create entry for the logical cloud resource in the database func (v *LogicalCloudClient) Create(project string, c LogicalCloud) (LogicalCloud, error) { - //Construct key consisting of name - key := LogicalCloudKey{ - Project: project, - LogicalCloudName: c.MetaData.LogicalCloudName, - } - - //Check if project exists - err := v.util.CheckProject(project) - if err != nil { - return LogicalCloud{}, pkgerrors.New("Unable to find the project") - } - - //Check if this Logical Cloud already exists - _, err = v.Get(project, c.MetaData.LogicalCloudName) - if err == nil { - return LogicalCloud{}, pkgerrors.New("Logical Cloud already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return LogicalCloud{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := LogicalCloudKey{ + Project: project, + LogicalCloudName: c.MetaData.LogicalCloudName, + } + + //Check if project exists + err := v.util.CheckProject(project) + if err != nil { + return LogicalCloud{}, pkgerrors.New("Unable to find the project") + } + + //Check if this Logical Cloud already exists + _, err = v.Get(project, c.MetaData.LogicalCloudName) + if err == nil { + return LogicalCloud{}, pkgerrors.New("Logical Cloud already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return LogicalCloud{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns Logical Cloud corresponding to logical cloud name func (v *LogicalCloudClient) Get(project, logicalCloudName string) (LogicalCloud, error) { - //Construct the composite key to select the entry - key := LogicalCloudKey{ - Project: project, - LogicalCloudName: logicalCloudName, - } - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Cloud") - } - - //value is a byte array - if value != nil { - lc := LogicalCloud{} - err = v.util.DBUnmarshal(value[0], &lc) - if err != nil { - return LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return lc, nil - } - - return LogicalCloud{}, pkgerrors.New("Error getting Logical Cloud") + //Construct the composite key to select the entry + key := LogicalCloudKey{ + Project: project, + LogicalCloudName: logicalCloudName, + } + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Cloud") + } + + //value is a byte array + if value != nil { + lc := LogicalCloud{} + err = v.util.DBUnmarshal(value[0], &lc) + if err != nil { + return LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return lc, nil + } + + return LogicalCloud{}, pkgerrors.New("Error getting Logical Cloud") } // GetAll returns Logical Clouds in the project func (v *LogicalCloudClient) GetAll(project string) ([]LogicalCloud, error) { - //Construct the composite key to select the entry - key := LogicalCloudKey{ - Project: project, - LogicalCloudName: "", - } - - var resp []LogicalCloud - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Clouds") - } - - for _, value := range values { - lc := LogicalCloud{} - err = v.util.DBUnmarshal(value, &lc) - if err != nil { - return []LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling values") - } - resp = append(resp, lc) - } - - return resp, nil + //Construct the composite key to select the entry + key := LogicalCloudKey{ + Project: project, + LogicalCloudName: "", + } + + var resp []LogicalCloud + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Clouds") + } + + for _, value := range values { + lc := LogicalCloud{} + err = v.util.DBUnmarshal(value, &lc) + if err != nil { + return []LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling values") + } + resp = append(resp, lc) + } + + return resp, nil } // Delete the Logical Cloud entry from database func (v *LogicalCloudClient) Delete(project, logicalCloudName string) error { - //Construct the composite key to select the entry - key := LogicalCloudKey{ - Project: project, - LogicalCloudName: logicalCloudName, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Logical Cloud") - } - - return nil + //Construct the composite key to select the entry + key := LogicalCloudKey{ + Project: project, + LogicalCloudName: logicalCloudName, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Logical Cloud") + } + + return nil } // Update an entry for the Logical Cloud in the database func (v *LogicalCloudClient) Update(project, logicalCloudName string, c LogicalCloud) (LogicalCloud, error) { - key := LogicalCloudKey{ - Project: project, - LogicalCloudName: logicalCloudName, - } - // Check for mismatch, logicalCloudName and payload logical cloud name - if c.MetaData.LogicalCloudName != logicalCloudName { - return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud name mismatch") - } - //Check if this Logical Cloud exists - _, err := v.Get(project, logicalCloudName) - if err != nil { - return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return LogicalCloud{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + key := LogicalCloudKey{ + Project: project, + LogicalCloudName: logicalCloudName, + } + // Check for mismatch, logicalCloudName and payload logical cloud name + if c.MetaData.LogicalCloudName != logicalCloudName { + return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud name mismatch") + } + //Check if this Logical Cloud exists + _, err := v.Get(project, logicalCloudName) + if err != nil { + return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return LogicalCloud{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } func (d DBService) DBInsert(storeName string, key db.Key, query interface{}, meta string, c interface{}) error { - err := db.DBconn.Insert(storeName, key, nil, meta, c) - if err != nil { - return pkgerrors.Wrap(err, "Creating DB Entry") - } + err := db.DBconn.Insert(storeName, key, nil, meta, c) + if err != nil { + return pkgerrors.Wrap(err, "Creating DB Entry") + } - return nil + return nil } func (d DBService) DBFind(storeName string, key db.Key, meta string) ([][]byte, error) { - value, err := db.DBconn.Find(storeName, key, meta) - if err != nil { - return [][]byte{}, pkgerrors.Wrap(err, "Get Resource") - } + value, err := db.DBconn.Find(storeName, key, meta) + if err != nil { + return [][]byte{}, pkgerrors.Wrap(err, "Get Resource") + } - return value, nil + return value, nil } func (d DBService) DBUnmarshal(value []byte, out interface{}) error { - err := db.DBconn.Unmarshal(value, out) - if err != nil { - return pkgerrors.Wrap(err, "Unmarshaling Value") - } + err := db.DBconn.Unmarshal(value, out) + if err != nil { + return pkgerrors.Wrap(err, "Unmarshaling Value") + } - return nil + return nil } func (d DBService) DBRemove(storeName string, key db.Key) error { - err := db.DBconn.Remove(storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Resource") - } + err := db.DBconn.Remove(storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Resource") + } - return nil + return nil } func (d DBService) CheckProject(project string) error { - // Check if project exists - _, err := module.NewProjectClient().GetProject(project) - if err != nil { - return pkgerrors.New("Unable to find the project") - } + // Check if project exists + _, err := module.NewProjectClient().GetProject(project) + if err != nil { + return pkgerrors.New("Unable to find the project") + } - return nil + return nil } func (d DBService) CheckLogicalCloud(project, logicalCloud string) error { - // Check if logical cloud exists - _, err := NewLogicalCloudClient().Get(project, logicalCloud) - if err != nil { - return pkgerrors.New("Unable to find the logical cloud") - } + // Check if logical cloud exists + _, err := NewLogicalCloudClient().Get(project, logicalCloud) + if err != nil { + return pkgerrors.New("Unable to find the logical cloud") + } - return nil + return nil } diff --git a/src/dcm/pkg/module/logicalcloud_test.go b/src/dcm/pkg/module/logicalcloud_test.go index 882cc292..fb205753 100644 --- a/src/dcm/pkg/module/logicalcloud_test.go +++ b/src/dcm/pkg/module/logicalcloud_test.go @@ -1,157 +1,156 @@ -package module
-
-import (
- "fmt"
- "testing"
-
- "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
- "github.com/stretchr/testify/mock"
- "github.com/pkg/errors"
-
-)
-
-type mockValues struct {
- mock.Mock
-}
-
-func (m *mockValues) DBInsert(name string, key db.Key, query interface {}, meta string, c interface {}) error{
- fmt.Println("Mocked Insert operation in Mongo")
- args := m.Called(name, key, nil, meta, c)
-
- return args.Error(0)
-}
-
-func (m *mockValues) DBFind(name string, key db.Key, meta string) ([][]byte, error) {
- fmt.Println("Mocked Mongo DB Find Operation")
- args := m.Called(name, key, meta)
-
- return args.Get(0).([][]byte), args.Error(1)
-}
-
-func (m *mockValues) DBUnmarshal(value []byte, out interface{}) error {
- fmt.Println("Mocked Mongo DB Unmarshal Operation")
- args := m.Called(value)
-
- return args.Error(0)
-}
-
-func (m *mockValues) DBRemove(name string, key db.Key) error {
- fmt.Println("Mocked Mongo DB Remove operation")
- args := m.Called(name, key)
-
- return args.Error(0)
-}
-
-func (m *mockValues) CheckProject(project string) error {
- fmt.Println("Mocked Check Project exists")
- args := m.Called(project)
-
- return args.Error(0)
-}
-
-func (m *mockValues) CheckLogicalCloud(project, logicalCloud string) error {
- fmt.Println("Mocked Check Logical Cloud exists")
- args := m.Called(project, logicalCloud)
-
- return args.Error(0)
-}
-
-func TestCreateLogicalCloud(t *testing.T) {
-
- mData := MetaDataList{
- LogicalCloudName: "test_asdf",
- }
-
- lc := LogicalCloud {
- MetaData: mData,
- }
- data1 := [][]byte{}
-
- key := LogicalCloudKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- }
- myMocks := new(mockValues)
- // just to get an error value
- err1 := errors.New("math: square root of negative number")
-
- myMocks.On("CheckProject", "test_project").Return(nil)
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1)
-
- lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks}
- _, err := lcClient.Create("test_project", lc)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestGetLogicalCloud(t *testing.T) {
- key := LogicalCloudKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- }
-
- data1 := [][]byte{
- []byte("abc"),
- }
-
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks}
- _, err := lcClient.Get("test_project", "test_asdf")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestDeleteLogicalCloud(t *testing.T) {
-
- key := LogicalCloudKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- }
-
- myMocks := new(mockValues)
-
- myMocks.On("DBRemove", "test_dcm", key).Return(nil)
-
- lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks}
- err := lcClient.Delete("test_project", "test_asdf")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-
-}
-
-func TestUpdateLogicalCloud(t *testing.T) {
- key := LogicalCloudKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- }
- mData := MetaDataList{
- LogicalCloudName: "test_asdf",
- }
- lc := LogicalCloud{
- MetaData: mData,
- }
- data1 := [][]byte{
- []byte("abc"),
- }
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks}
- _, err := lcClient.Update("test_project", "test_asdf", lc)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
+package module + +import ( + "fmt" + "testing" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + "github.com/pkg/errors" + "github.com/stretchr/testify/mock" +) + +type mockValues struct { + mock.Mock +} + +func (m *mockValues) DBInsert(name string, key db.Key, query interface{}, meta string, c interface{}) error { + fmt.Println("Mocked Insert operation in Mongo") + args := m.Called(name, key, nil, meta, c) + + return args.Error(0) +} + +func (m *mockValues) DBFind(name string, key db.Key, meta string) ([][]byte, error) { + fmt.Println("Mocked Mongo DB Find Operation") + args := m.Called(name, key, meta) + + return args.Get(0).([][]byte), args.Error(1) +} + +func (m *mockValues) DBUnmarshal(value []byte, out interface{}) error { + fmt.Println("Mocked Mongo DB Unmarshal Operation") + args := m.Called(value) + + return args.Error(0) +} + +func (m *mockValues) DBRemove(name string, key db.Key) error { + fmt.Println("Mocked Mongo DB Remove operation") + args := m.Called(name, key) + + return args.Error(0) +} + +func (m *mockValues) CheckProject(project string) error { + fmt.Println("Mocked Check Project exists") + args := m.Called(project) + + return args.Error(0) +} + +func (m *mockValues) CheckLogicalCloud(project, logicalCloud string) error { + fmt.Println("Mocked Check Logical Cloud exists") + args := m.Called(project, logicalCloud) + + return args.Error(0) +} + +func TestCreateLogicalCloud(t *testing.T) { + + mData := MetaDataList{ + LogicalCloudName: "test_asdf", + } + + lc := LogicalCloud{ + MetaData: mData, + } + data1 := [][]byte{} + + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + _, err := lcClient.Create("test_project", lc) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetLogicalCloud(t *testing.T) { + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + _, err := lcClient.Get("test_project", "test_asdf") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteLogicalCloud(t *testing.T) { + + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + err := lcClient.Delete("test_project", "test_asdf") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateLogicalCloud(t *testing.T) { + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + mData := MetaDataList{ + LogicalCloudName: "test_asdf", + } + lc := LogicalCloud{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + _, err := lcClient.Update("test_project", "test_asdf", lc) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/module.go b/src/dcm/pkg/module/module.go index 293f6dd5..70e348e7 100644 --- a/src/dcm/pkg/module/module.go +++ b/src/dcm/pkg/module/module.go @@ -18,22 +18,22 @@ package module // Client for using the services in the orchestrator type Client struct { - LogicalCloud *LogicalCloudClient - Cluster *ClusterClient - Quota *QuotaClient - UserPermission *UserPermissionClient - KeyValue *KeyValueClient - // Add Clients for API's here + LogicalCloud *LogicalCloudClient + Cluster *ClusterClient + Quota *QuotaClient + UserPermission *UserPermissionClient + KeyValue *KeyValueClient + // Add Clients for API's here } // NewClient creates a new client for using the services func NewClient() *Client { - c := &Client{} - c.LogicalCloud = NewLogicalCloudClient() - c.Cluster = NewClusterClient() - c.Quota = NewQuotaClient() - c.UserPermission = NewUserPermissionClient() - c.KeyValue = NewKeyValueClient() - // Add Client API handlers here - return c + c := &Client{} + c.LogicalCloud = NewLogicalCloudClient() + c.Cluster = NewClusterClient() + c.Quota = NewQuotaClient() + c.UserPermission = NewUserPermissionClient() + c.KeyValue = NewKeyValueClient() + // Add Client API handlers here + return c } diff --git a/src/dcm/pkg/module/quota.go b/src/dcm/pkg/module/quota.go index 1a7012f6..cbd9c8b7 100644 --- a/src/dcm/pkg/module/quota.go +++ b/src/dcm/pkg/module/quota.go @@ -12,211 +12,210 @@ * 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 module import ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) -// Quota contains the parameters needed for a Quota +// Quota contains the parameters needed for a Quota type Quota struct { - MetaData QMetaDataList `json:"metadata"` - Specification QSpec `json:"spec"` + MetaData QMetaDataList `json:"metadata"` + Specification QSpec `json:"spec"` } - // MetaData contains the parameters needed for metadata type QMetaDataList struct { - QuotaName string `json:"name"` - Description string `json:"description"` + QuotaName string `json:"name"` + Description string `json:"description"` } // Spec contains the parameters needed for spec type QSpec struct { - LimitsCPU string `json:"limits.cpu"` - LimitsMemory string `json:"limits.memory"` - RequestsCPU string `json:"requests.cpu"` - RequestsMemory string `json:"requests.memory"` - RequestsStorage string `json:"requests.storage"` - LimitsEphemeralStorage string `json:"limits.ephemeral.storage"` - PersistentVolumeClaims string `json:"persistentvolumeclaims"` - Pods string `json:"pods"` - ConfigMaps string `json:"configmaps"` - ReplicationControllers string `json:"replicationcontrollers"` - ResourceQuotas string `json:"resourcequotas"` - Services string `json:"services"` - ServicesLoadBalancers string `json:"services.loadbalancers"` - ServicesNodePorts string `json:"services.nodeports"` - Secrets string `json:"secrets"` - CountReplicationControllers string `json:"count/replicationcontrollers"` - CountDeploymentsApps string `json:"count/deployments.apps"` - CountReplicasetsApps string `json:"count/replicasets.apps"` - CountStatefulSets string `json:"count/statefulsets.apps"` - CountJobsBatch string `json:"count/jobs.batch"` - CountCronJobsBatch string `json:"count/cronjobs.batch"` - CountDeploymentsExtensions string `json:"count/deployments.extensions"` + LimitsCPU string `json:"limits.cpu"` + LimitsMemory string `json:"limits.memory"` + RequestsCPU string `json:"requests.cpu"` + RequestsMemory string `json:"requests.memory"` + RequestsStorage string `json:"requests.storage"` + LimitsEphemeralStorage string `json:"limits.ephemeral.storage"` + PersistentVolumeClaims string `json:"persistentvolumeclaims"` + Pods string `json:"pods"` + ConfigMaps string `json:"configmaps"` + ReplicationControllers string `json:"replicationcontrollers"` + ResourceQuotas string `json:"resourcequotas"` + Services string `json:"services"` + ServicesLoadBalancers string `json:"services.loadbalancers"` + ServicesNodePorts string `json:"services.nodeports"` + Secrets string `json:"secrets"` + CountReplicationControllers string `json:"count/replicationcontrollers"` + CountDeploymentsApps string `json:"count/deployments.apps"` + CountReplicasetsApps string `json:"count/replicasets.apps"` + CountStatefulSets string `json:"count/statefulsets.apps"` + CountJobsBatch string `json:"count/jobs.batch"` + CountCronJobsBatch string `json:"count/cronjobs.batch"` + CountDeploymentsExtensions string `json:"count/deployments.extensions"` } // QuotaKey is the key structure that is used in the database type QuotaKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - QuotaName string `json:"qname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + QuotaName string `json:"qname"` } // QuotaManager is an interface that exposes the connection // functionality type QuotaManager interface { - CreateQuota(project, logicalCloud string, c Quota) (Quota, error) - GetQuota(project, logicalCloud, name string) (Quota, error) - GetAllQuotas(project, logicalCloud string) ([]Quota, error) - DeleteQuota(project, logicalCloud, name string) error - UpdateQuota(project, logicalCloud, name string, c Quota) (Quota, error) + CreateQuota(project, logicalCloud string, c Quota) (Quota, error) + GetQuota(project, logicalCloud, name string) (Quota, error) + GetAllQuotas(project, logicalCloud string) ([]Quota, error) + DeleteQuota(project, logicalCloud, name string) error + UpdateQuota(project, logicalCloud, name string, c Quota) (Quota, error) } // QuotaClient implements the QuotaManager // It will also be used to maintain some localized state type QuotaClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // QuotaClient returns an instance of the QuotaClient // which implements the QuotaManager func NewQuotaClient() *QuotaClient { - service := DBService{} - return &QuotaClient{ - storeName: "orchestrator", - tagMeta: "quota", - util: service, - } + service := DBService{} + return &QuotaClient{ + storeName: "orchestrator", + tagMeta: "quota", + util: service, + } } // Create entry for the quota resource in the database func (v *QuotaClient) CreateQuota(project, logicalCloud string, c Quota) (Quota, error) { - //Construct key consisting of name - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: c.MetaData.QuotaName, - } - - //Check if project exists - err := v.util.CheckProject(project) - if err != nil { - return Quota{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return Quota{}, pkgerrors.New("Unable to find the logical cloud") - } - //Check if this Quota already exists - _, err = v.GetQuota(project, logicalCloud, c.MetaData.QuotaName) - if err == nil { - return Quota{}, pkgerrors.New("Quota already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: c.MetaData.QuotaName, + } + + //Check if project exists + err := v.util.CheckProject(project) + if err != nil { + return Quota{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return Quota{}, pkgerrors.New("Unable to find the logical cloud") + } + //Check if this Quota already exists + _, err = v.GetQuota(project, logicalCloud, c.MetaData.QuotaName) + if err == nil { + return Quota{}, pkgerrors.New("Quota already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns Quota for corresponding quota name func (v *QuotaClient) GetQuota(project, logicalCloud, quotaName string) (Quota, error) { - //Construct the composite key to select the entry - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: quotaName, - } - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Quota") - } - - //value is a byte array - if value != nil { - q := Quota{} - err = v.util.DBUnmarshal(value[0], &q) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return q, nil - } - - return Quota{}, pkgerrors.New("Error getting Quota") + //Construct the composite key to select the entry + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: quotaName, + } + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Quota") + } + + //value is a byte array + if value != nil { + q := Quota{} + err = v.util.DBUnmarshal(value[0], &q) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return q, nil + } + + return Quota{}, pkgerrors.New("Error getting Quota") } // GetAll returns all cluster quotas in the logical cloud func (v *QuotaClient) GetAllQuotas(project, logicalCloud string) ([]Quota, error) { - //Construct the composite key to select the entry - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: "", - } - var resp []Quota - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []Quota{}, pkgerrors.Wrap(err, "Get All Quotas") - } - - for _, value := range values { - q := Quota{} - err = v.util.DBUnmarshal(value, &q) - if err != nil { - return []Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - resp = append(resp, q) - } - - return resp, nil + //Construct the composite key to select the entry + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: "", + } + var resp []Quota + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []Quota{}, pkgerrors.Wrap(err, "Get All Quotas") + } + + for _, value := range values { + q := Quota{} + err = v.util.DBUnmarshal(value, &q) + if err != nil { + return []Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + resp = append(resp, q) + } + + return resp, nil } // Delete the Quota entry from database func (v *QuotaClient) DeleteQuota(project, logicalCloud, quotaName string) error { - //Construct the composite key to select the entry - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: quotaName, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Quota") - } - return nil + //Construct the composite key to select the entry + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: quotaName, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Quota") + } + return nil } // Update an entry for the Quota in the database func (v *QuotaClient) UpdateQuota(project, logicalCloud, quotaName string, c Quota) (Quota, error) { - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: quotaName, - } - //Check quota URL name against the quota json name - if c.MetaData.QuotaName != quotaName { - return Quota{}, pkgerrors.New("Update Error - Quota name mismatch") - } - //Check if this Quota exists - _, err := v.GetQuota(project, logicalCloud, quotaName) - if err != nil { - return Quota{}, pkgerrors.New("Update Error - Quota doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: quotaName, + } + //Check quota URL name against the quota json name + if c.MetaData.QuotaName != quotaName { + return Quota{}, pkgerrors.New("Update Error - Quota name mismatch") + } + //Check if this Quota exists + _, err := v.GetQuota(project, logicalCloud, quotaName) + if err != nil { + return Quota{}, pkgerrors.New("Update Error - Quota doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/quota_test.go b/src/dcm/pkg/module/quota_test.go index 87a60d71..5b70cf77 100644 --- a/src/dcm/pkg/module/quota_test.go +++ b/src/dcm/pkg/module/quota_test.go @@ -1,115 +1,112 @@ -package module
-
-import (
- "testing"
-
- "github.com/pkg/errors"
-
-)
-
-
-func TestCreateQuota(t *testing.T) {
-
- mData := QMetaDataList{
- QuotaName: "test_quota",
- }
-
- q := Quota {
- MetaData: mData,
- }
- data1 := [][]byte{}
-
-
- key := QuotaKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- QuotaName: "test_quota",
- }
- myMocks := new(mockValues)
- // just to get an error value
- err1 := errors.New("math: square root of negative number")
-
- myMocks.On("CheckProject", "test_project").Return(nil)
- myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil)
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1)
-
- qClient := QuotaClient{"test_dcm", "test_meta", myMocks}
- _, err := qClient.CreateQuota("test_project", "test_asdf", q)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestGetQuota(t *testing.T) {
- key := QuotaKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- QuotaName: "test_quota",
- }
-
- data1 := [][]byte{
- []byte("abc"),
- }
-
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- qClient := QuotaClient{"test_dcm", "test_meta", myMocks}
- _, err := qClient.GetQuota("test_project", "test_asdf", "test_quota")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestDeleteQuota(t *testing.T) {
-
- key := QuotaKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- QuotaName: "test_quota",
- }
-
- myMocks := new(mockValues)
-
- myMocks.On("DBRemove", "test_dcm", key).Return(nil)
-
- qClient := QuotaClient{"test_dcm", "test_meta", myMocks}
- err := qClient.DeleteQuota("test_project", "test_asdf", "test_quota")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-
-}
-
-func TestUpdateQuota(t *testing.T) {
- key := QuotaKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- QuotaName: "test_quota",
- }
- mData := QMetaDataList{
- QuotaName: "test_quota",
- }
- q := Quota{
- MetaData: mData,
- }
- data1 := [][]byte{
- []byte("abc"),
- }
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- qClient := QuotaClient{"test_dcm", "test_meta", myMocks}
- _, err := qClient.UpdateQuota("test_project", "test_asdf", "test_quota", q)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
+package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateQuota(t *testing.T) { + + mData := QMetaDataList{ + QuotaName: "test_quota", + } + + q := Quota{ + MetaData: mData, + } + data1 := [][]byte{} + + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + _, err := qClient.CreateQuota("test_project", "test_asdf", q) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetQuota(t *testing.T) { + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + _, err := qClient.GetQuota("test_project", "test_asdf", "test_quota") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteQuota(t *testing.T) { + + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + err := qClient.DeleteQuota("test_project", "test_asdf", "test_quota") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateQuota(t *testing.T) { + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + mData := QMetaDataList{ + QuotaName: "test_quota", + } + q := Quota{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + _, err := qClient.UpdateQuota("test_project", "test_asdf", "test_quota", q) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/userpermissions.go b/src/dcm/pkg/module/userpermissions.go index cf961a65..2cff712b 100644 --- a/src/dcm/pkg/module/userpermissions.go +++ b/src/dcm/pkg/module/userpermissions.go @@ -12,182 +12,183 @@ * 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 module import ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) // UserPermission contains the parameters needed for a user permission type UserPermission struct { - UserPermissionName string `json:"name"` - APIGroups []string `json:"apiGroups"` - Resources []string `json:"resources"` - Verbs []string `json:"verbs"` + UserPermissionName string `json:"name"` + APIGroups []string `json:"apiGroups"` + Resources []string `json:"resources"` + Verbs []string `json:"verbs"` } // UserPermissionKey is the key structure that is used in the database type UserPermissionKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - UserPermissionName string `json:"upname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + UserPermissionName string `json:"upname"` } // UserPermissionManager is an interface that exposes the connection // functionality type UserPermissionManager interface { - CreateUserPerm(project, logicalCloud string, c UserPermission) (UserPermission, error) - GetUserPerm(project, logicalCloud, name string) (UserPermission, error) - GetAllUserPerms(project, logicalCloud string) ([]UserPermission, error) - DeleteUserPerm(project, logicalCloud, name string) error - UpdateUserPerm(project, logicalCloud, name string, c UserPermission) (UserPermission, error) + CreateUserPerm(project, logicalCloud string, c UserPermission) (UserPermission, error) + GetUserPerm(project, logicalCloud, name string) (UserPermission, error) + GetAllUserPerms(project, logicalCloud string) ([]UserPermission, error) + DeleteUserPerm(project, logicalCloud, name string) error + UpdateUserPerm(project, logicalCloud, name string, c UserPermission) (UserPermission, error) } // UserPermissionClient implements the UserPermissionManager // It will also be used to maintain some localized state type UserPermissionClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // UserPermissionClient returns an instance of the UserPermissionClient // which implements the UserPermissionManager func NewUserPermissionClient() *UserPermissionClient { - service := DBService{} - return &UserPermissionClient{ - storeName: "orchestrator", - tagMeta: "userpermission", - util: service, - } + service := DBService{} + return &UserPermissionClient{ + storeName: "orchestrator", + tagMeta: "userpermission", + util: service, + } } // Create entry for the User Permission resource in the database func (v *UserPermissionClient) CreateUserPerm(project, logicalCloud string, c UserPermission) (UserPermission, error) { - //Construct key consisting of name - key := UserPermissionKey { - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: c.UserPermissionName, - } - - //Check if project exists - err := v.util.CheckProject(project) - if err != nil { - return UserPermission{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return UserPermission{}, pkgerrors.New("Unable to find the logical cloud") - } - - //Check if this User Permission already exists - _, err = v.GetUserPerm(project, logicalCloud, c.UserPermissionName) - if err == nil { - return UserPermission{}, pkgerrors.New("User Permission already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: c.UserPermissionName, + } + + //Check if project exists + err := v.util.CheckProject(project) + if err != nil { + return UserPermission{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return UserPermission{}, pkgerrors.New("Unable to find the logical cloud") + } + + //Check if this User Permission already exists + _, err = v.GetUserPerm(project, logicalCloud, c.UserPermissionName) + if err == nil { + return UserPermission{}, pkgerrors.New("User Permission already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns User Permission for corresponding name func (v *UserPermissionClient) GetUserPerm(project, logicalCloud, userPermName string) (UserPermission, error) { - //Construct the composite key to select the entry - key := UserPermissionKey{ - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: userPermName, - } - - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Get User Permission") - } - - //value is a byte array - if value != nil { - up := UserPermission{} - err = v.util.DBUnmarshal(value[0], &up) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return up, nil - } - - return UserPermission{}, pkgerrors.New("Error getting User Permission") + //Construct the composite key to select the entry + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: userPermName, + } + + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Get User Permission") + } + + //value is a byte array + if value != nil { + up := UserPermission{} + err = v.util.DBUnmarshal(value[0], &up) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return up, nil + } + + return UserPermission{}, pkgerrors.New("Error getting User Permission") } // GetAll lists all user permissions func (v *UserPermissionClient) GetAllUserPerms(project, logicalCloud string) ([]UserPermission, error) { - //Construct the composite key to select the entry - key := UserPermissionKey { - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: "", - } - var resp []UserPermission - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []UserPermission{}, pkgerrors.Wrap(err, "Get All User Permissions") - } - - for _, value := range values { - up := UserPermission{} - err = v.util.DBUnmarshal(value, &up) - if err != nil { - return []UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - resp = append(resp, up) - } - return resp, nil + //Construct the composite key to select the entry + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: "", + } + var resp []UserPermission + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []UserPermission{}, pkgerrors.Wrap(err, "Get All User Permissions") + } + + for _, value := range values { + up := UserPermission{} + err = v.util.DBUnmarshal(value, &up) + if err != nil { + return []UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + resp = append(resp, up) + } + return resp, nil } + // Delete the User Permission entry from database func (v *UserPermissionClient) DeleteUserPerm(project, logicalCloud, userPermName string) error { - //Construct the composite key to select the entry - key := UserPermissionKey{ - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: userPermName, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete User Permission") - } - return nil + //Construct the composite key to select the entry + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: userPermName, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete User Permission") + } + return nil } // Update an entry for the User Permission in the database func (v *UserPermissionClient) UpdateUserPerm(project, logicalCloud, userPermName string, c UserPermission) ( - UserPermission, error) { - - key := UserPermissionKey{ - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: userPermName, - } - //Check for URL name and json permission name mismatch - if c.UserPermissionName != userPermName { - return UserPermission{}, pkgerrors.New("Update Error - Permission name mismatch") - } - //Check if this User Permission exists - _, err := v.GetUserPerm(project, logicalCloud, userPermName) - if err != nil { - return UserPermission{}, pkgerrors.New( - "Update Error - User Permission doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + UserPermission, error) { + + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: userPermName, + } + //Check for URL name and json permission name mismatch + if c.UserPermissionName != userPermName { + return UserPermission{}, pkgerrors.New("Update Error - Permission name mismatch") + } + //Check if this User Permission exists + _, err := v.GetUserPerm(project, logicalCloud, userPermName) + if err != nil { + return UserPermission{}, pkgerrors.New( + "Update Error - User Permission doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/userpermissions_test.go b/src/dcm/pkg/module/userpermissions_test.go index f134aa0f..5ada2303 100644 --- a/src/dcm/pkg/module/userpermissions_test.go +++ b/src/dcm/pkg/module/userpermissions_test.go @@ -1,110 +1,108 @@ -package module
-
-import (
- "testing"
-
- "github.com/pkg/errors"
-
-)
-
-
-func TestCreateUserPerm(t *testing.T) {
-
- up := UserPermission {
- UserPermissionName: "test_user_perm",
- }
- data1 := [][]byte{}
-
- // data2 := []byte("abc")
-
- key := UserPermissionKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- UserPermissionName: "test_user_perm",
- }
- myMocks := new(mockValues)
- // just to get an error value
- err1 := errors.New("math: square root of negative number")
-
- myMocks.On("CheckProject", "test_project").Return(nil)
- myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil)
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1)
- // myMocks.On("DBUnmarshal", data2).Return(nil)
-
- upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks}
- _, err := upClient.CreateUserPerm("test_project", "test_asdf", up)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestGetUserPerm(t *testing.T) {
- key := UserPermissionKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- UserPermissionName: "test_user_perm",
- }
-
- data1 := [][]byte{
- []byte("abc"),
- }
-
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks}
- _, err := upClient.GetUserPerm("test_project", "test_asdf", "test_user_perm")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
-
-func TestDeleteUserPerm(t *testing.T) {
-
- key := UserPermissionKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- UserPermissionName: "test_user_perm",
- }
-
- myMocks := new(mockValues)
-
- myMocks.On("DBRemove", "test_dcm", key).Return(nil)
-
- upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks}
- err := upClient.DeleteUserPerm("test_project", "test_asdf", "test_user_perm")
- if err != nil {
- t.Errorf("Some error occured!")
- }
-
-}
-
-func TestUpdateUserPerm(t *testing.T) {
- key := UserPermissionKey{
- Project: "test_project",
- LogicalCloudName: "test_asdf",
- UserPermissionName: "test_user_perm",
- }
- up := UserPermission{
- UserPermissionName: "test_user_perm",
- }
- data1 := [][]byte{
- []byte("abc"),
- }
- data2 := []byte("abc")
-
- myMocks := new(mockValues)
-
- myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil)
- myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
- myMocks.On("DBUnmarshal", data2).Return(nil)
- upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks}
- _, err := upClient.UpdateUserPerm("test_project", "test_asdf", "test_user_perm", up)
- if err != nil {
- t.Errorf("Some error occured!")
- }
-}
+package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateUserPerm(t *testing.T) { + + up := UserPermission{ + UserPermissionName: "test_user_perm", + } + data1 := [][]byte{} + + // data2 := []byte("abc") + + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + // myMocks.On("DBUnmarshal", data2).Return(nil) + + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + _, err := upClient.CreateUserPerm("test_project", "test_asdf", up) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetUserPerm(t *testing.T) { + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + _, err := upClient.GetUserPerm("test_project", "test_asdf", "test_user_perm") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteUserPerm(t *testing.T) { + + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + err := upClient.DeleteUserPerm("test_project", "test_asdf", "test_user_perm") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateUserPerm(t *testing.T) { + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + up := UserPermission{ + UserPermissionName: "test_user_perm", + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + _, err := upClient.UpdateUserPerm("test_project", "test_asdf", "test_user_perm", up) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/test/dcm_call_api.sh b/src/dcm/test/dcm_call_api.sh index 3bf27524..966bc3d6 100755 --- a/src/dcm/test/dcm_call_api.sh +++ b/src/dcm/test/dcm_call_api.sh @@ -15,11 +15,24 @@ # */ +dcm_addr="http://localhost:9077" + +# parameters project="test-project" description="test-description" logical_cloud_name="lc1" -logical_cloud_url="http://localhost:9015/v2/projects/${project}/logical-clouds" +namespace="ns1" +user="user-1" +permission="permission-1" +cluster_provider_name="cp-1" +cluster_1_name="c1" +cluster_1_name="c2" +lc_cluster_1_name="lc-cl-1" +lc_cluster_2_name="lc-cl-2" quota_name="quota-1" + +# endpoints +logical_cloud_url="$dcm_addr/v2/projects/${project}/logical-clouds" quota_url="${logical_cloud_url}/${logical_cloud_name}/cluster-quotas" cluster_url="${logical_cloud_url}/${logical_cloud_name}/cluster-references" @@ -28,17 +41,17 @@ logical_cloud_data="$(cat << EOF { "metadata" : { "name": "${logical_cloud_name}", - "description": "${test-description}", + "description": "${description}", "userData1":"<user data>", "userData2":"<user data>" }, "spec" : { - "namespace" : "ns-1", + "namespace" : "${namespace}", "user" : { - "user-name" : "user-1", + "user-name" : "${user}", "type" : "certificate", "user-permissions" : [ - { "permission-name" : "permission-1", + { "permission-name" : "${permission}", "apiGroups" : ["stable.example.com"], "resources" : ["secrets", "pods"], "verbs" : ["get", "watch", "list", "create"] @@ -53,15 +66,15 @@ EOF cluster_1_data="$(cat << EOF { "metadata" : { - "name": "lc-cl-1", - "description": "${test-description}", + "name": "${lc_cluster_1_name}", + "description": "${description}", "userData1":"<user data>", "userData2":"<user data>" }, "spec" : { - "cluster-provider": "cp-1", - "cluster-name": "c1", + "cluster-provider": "${cluster_provider_name}", + "cluster-name": "${cluster_1_name}", "loadbalancer-ip" : "0.0.0.0" } } @@ -71,26 +84,47 @@ EOF cluster_2_data="$(cat << EOF { "metadata" : { - "name": "lc-cl-2", - "description": "${test-description}", + "name": "${lc_cluster_2_name}", + "description": "${description}", "userData1":"<user data>", "userData2":"<user data>" }, "spec" : { - "cluster-provider": "cp-1", - "cluster-name": "c2", + "cluster-provider": "${cluster_provider_name}", + "cluster-name": "${cluster_2_name}", "loadbalancer-ip" : "0.0.0.1" } } EOF )" +# removed all special chars from quota spec keys +# due to loss of data when unmarshalling from json quota_data="$(cat << EOF { "metadata" : { "name" : "${quota_name}", - "description": "${test-description}" + "description": "${description}" + }, + "spec" : { + "persistentvolumeclaims" : "10", + "pods": "500", + "configmaps" : "10", + "replicationcontrollers": "10", + "resourcequotas" : "10", + "services": "10", + "secrets" : "10" + } +} +EOF +)" + +quota_data_original="$(cat << EOF +{ + "metadata" : { + "name" : "${quota_name}", + "description": "${description}" }, "spec" : { "limits.cpu": "400", @@ -143,5 +177,24 @@ curl -X GET "${logical_cloud_url}/${logical_cloud_name}" printf "\n\nGetting clusters info for logical cloud\n\n" curl -X GET ${cluster_url} +printf "\n\nGetting first cluster of logical cloud\n" +curl -X GET ${cluster_url}/${lc_cluster_1_name} + +printf "\n\nGetting second cluster of logical cloud\n" +curl -X GET ${cluster_url}/${lc_cluster_2_name} + printf "\n\nGetting Quota info for the logical cloud\n\n" -curl -X GET "${quota_url}/${quota_name}"
\ No newline at end of file +curl -X GET "${quota_url}/${quota_name}" + +# Cleanup (delete created resources) +if [ "$1" == "clean" ]; then + printf "\n\nDeleting Quota info for the logical cloud\n\n" + curl -X DELETE "${quota_url}/${quota_name}" + + printf "\n\nDeleting the two clusters from logical cloud\n\n" + curl -X DELETE ${cluster_url}/${lc_cluster_1_name} + curl -X DELETE ${cluster_url}/${lc_cluster_2_name} + + printf "\n\nDeleting logical cloud data\n\n" + curl -X DELETE ${logical_cloud_url}/${logical_cloud_name} +fi diff --git a/src/ncm/pkg/module/types/module_definitions.go b/src/ncm/pkg/module/types/module_definitions.go index 0dd657ac..0c85cdb1 100644 --- a/src/ncm/pkg/module/types/module_definitions.go +++ b/src/ncm/pkg/module/types/module_definitions.go @@ -20,5 +20,5 @@ type ClientDbInfo struct { StoreName string // name of the mongodb collection to use for client documents TagMeta string // attribute key name for the json data of a client document TagContent string // attribute key name for the file data of a client document - TagContext string // attribute key name for context object in App Context + TagState string // attribute key name for context object in App Context } diff --git a/src/ncm/pkg/networkintents/network.go b/src/ncm/pkg/networkintents/network.go index de8ee504..58480cc8 100644 --- a/src/ncm/pkg/networkintents/network.go +++ b/src/ncm/pkg/networkintents/network.go @@ -22,6 +22,7 @@ import ( nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -89,11 +90,25 @@ func (v *NetworkClient) CreateNetwork(p Network, clusterProvider, cluster string NetworkName: p.Metadata.Name, } - //Check if cluster exists - _, err := clusterPkg.NewClusterClient().GetCluster(clusterProvider, cluster) + //Check if cluster exists and in a state for adding network intents + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) if err != nil { return Network{}, pkgerrors.New("Unable to find the cluster") } + switch s.State { + case state.StateEnum.Approved: + return Network{}, pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return Network{}, pkgerrors.Errorf("Existing cluster network intents must be terminated before creating: " + cluster) + case state.StateEnum.Instantiated: + return Network{}, pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Instantiated) + default: + return Network{}, pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + s.State) + } //Check if this Network already exists _, err = v.GetNetwork(p.Metadata.Name, clusterProvider, cluster) @@ -167,6 +182,25 @@ func (v *NetworkClient) GetNetworks(clusterProvider, cluster string) ([]Network, // Delete the Network from database func (v *NetworkClient) DeleteNetwork(name, clusterProvider, cluster string) error { + // verify cluster is in a state where network intent can be deleted + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) + if err != nil { + return pkgerrors.New("Unable to find the cluster") + } + switch s.State { + case state.StateEnum.Approved: + return pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return pkgerrors.Errorf("Cluster network intents must be terminated before deleting: " + cluster) + case state.StateEnum.Instantiated: + return pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + state.StateEnum.Instantiated) + default: + return pkgerrors.Errorf("Cluster is in an invalid state: " + cluster + " " + s.State) + } //Construct key and tag to select the entry key := NetworkKey{ @@ -175,7 +209,7 @@ func (v *NetworkClient) DeleteNetwork(name, clusterProvider, cluster string) err NetworkName: name, } - err := db.DBconn.Remove(v.db.StoreName, key) + err = db.DBconn.Remove(v.db.StoreName, key) if err != nil { return pkgerrors.Wrap(err, "Delete Network Entry;") } diff --git a/src/ncm/pkg/networkintents/providernet.go b/src/ncm/pkg/networkintents/providernet.go index 072e07f6..dbe6e46c 100644 --- a/src/ncm/pkg/networkintents/providernet.go +++ b/src/ncm/pkg/networkintents/providernet.go @@ -22,6 +22,7 @@ import ( nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -84,6 +85,26 @@ func NewProviderNetClient() *ProviderNetClient { // CreateProviderNet - create a new ProviderNet func (v *ProviderNetClient) CreateProviderNet(p ProviderNet, clusterProvider, cluster string, exists bool) (ProviderNet, error) { + // verify cluster exists and in state to add provider networks + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) + if err != nil { + return ProviderNet{}, pkgerrors.New("Unable to find the cluster") + } + switch s.State { + case state.StateEnum.Approved: + return ProviderNet{}, pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return ProviderNet{}, pkgerrors.Wrap(err, "Existing cluster provider network intents must be terminated before creating: "+cluster) + case state.StateEnum.Instantiated: + return ProviderNet{}, pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Instantiated) + default: + return ProviderNet{}, pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+s.State) + } + //Construct key and tag to select the entry key := ProviderNetKey{ ClusterProviderName: clusterProvider, @@ -91,12 +112,6 @@ func (v *ProviderNetClient) CreateProviderNet(p ProviderNet, clusterProvider, cl ProviderNetName: p.Metadata.Name, } - //Check if cluster exists - _, err := clusterPkg.NewClusterClient().GetCluster(clusterProvider, cluster) - if err != nil { - return ProviderNet{}, pkgerrors.New("Unable to find the cluster") - } - //Check if this ProviderNet already exists _, err = v.GetProviderNet(p.Metadata.Name, clusterProvider, cluster) if err == nil && !exists { @@ -169,6 +184,25 @@ func (v *ProviderNetClient) GetProviderNets(clusterProvider, cluster string) ([] // Delete the ProviderNet from database func (v *ProviderNetClient) DeleteProviderNet(name, clusterProvider, cluster string) error { + // verify cluster is in a state where provider network intent can be deleted + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) + if err != nil { + return pkgerrors.New("Unable to find the cluster") + } + switch s.State { + case state.StateEnum.Approved: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return pkgerrors.Wrap(err, "Cluster provider network intents must be terminated before deleting: "+cluster) + case state.StateEnum.Instantiated: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Instantiated) + default: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+s.State) + } //Construct key and tag to select the entry key := ProviderNetKey{ @@ -177,7 +211,7 @@ func (v *ProviderNetClient) DeleteProviderNet(name, clusterProvider, cluster str ProviderNetName: name, } - err := db.DBconn.Remove(v.db.StoreName, key) + err = db.DBconn.Remove(v.db.StoreName, key) if err != nil { return pkgerrors.Wrap(err, "Delete ProviderNet Entry;") } diff --git a/src/ncm/pkg/scheduler/scheduler.go b/src/ncm/pkg/scheduler/scheduler.go index 4886a67e..8ced68b8 100644 --- a/src/ncm/pkg/scheduler/scheduler.go +++ b/src/ncm/pkg/scheduler/scheduler.go @@ -27,6 +27,7 @@ import ( "github.com/onap/multicloud-k8s/src/orchestrator/pkg/grpc/installappclient" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -51,7 +52,7 @@ func NewSchedulerClient() *SchedulerClient { StoreName: "cluster", TagMeta: "clustermetadata", TagContent: "clustercontent", - TagContext: "clustercontext", + TagState: "stateInfo", }, } } @@ -59,9 +60,23 @@ func NewSchedulerClient() *SchedulerClient { // Apply Network Intents associated with a cluster func (v *SchedulerClient) ApplyNetworkIntents(clusterProvider, cluster string) error { - _, _, err := clusterPkg.NewClusterClient().GetClusterContext(clusterProvider, cluster) - if err == nil { - return pkgerrors.Errorf("Cluster network intents have already been applied: %v, %v", clusterProvider, cluster) + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) + if err != nil { + return pkgerrors.Errorf("Error finding cluster: %v %v", clusterProvider, cluster) + } + switch s.State { + case state.StateEnum.Approved: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Approved) + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return nil + case state.StateEnum.Instantiated: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Instantiated) + default: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+s.State) } // Make an app context for the network intent resources @@ -135,12 +150,17 @@ func (v *SchedulerClient) ApplyNetworkIntents(clusterProvider, cluster string) e return pkgerrors.Wrap(err, "Error adding Cluster to AppContext") } - // save the context in the cluster db record + // update the StateInfo in the cluster db record key := clusterPkg.ClusterKey{ ClusterProviderName: clusterProvider, ClusterName: cluster, } - err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagContext, ctxVal) + stateInfo := state.StateInfo{ + State: state.StateEnum.Applied, + ContextId: ctxVal.(string), + } + + err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagState, stateInfo) if err != nil { cleanuperr := ac.DeleteCompositeApp() if cleanuperr != nil { @@ -149,7 +169,7 @@ func (v *SchedulerClient) ApplyNetworkIntents(clusterProvider, cluster string) e "cluster": cluster, }) } - return pkgerrors.Wrap(err, "Error adding AppContext to DB") + return pkgerrors.Wrap(err, "Error updating the stateInfo of cluster: "+cluster) } // call resource synchronizer to instantiate the CRs in the cluster @@ -163,37 +183,55 @@ func (v *SchedulerClient) ApplyNetworkIntents(clusterProvider, cluster string) e // Terminate Network Intents associated with a cluster func (v *SchedulerClient) TerminateNetworkIntents(clusterProvider, cluster string) error { - context, ctxVal, err := clusterPkg.NewClusterClient().GetClusterContext(clusterProvider, cluster) + s, err := clusterPkg.NewClusterClient().GetClusterState(clusterProvider, cluster) if err != nil { - return pkgerrors.Wrapf(err, "Error finding AppContext for cluster: %v, %v", clusterProvider, cluster) + return pkgerrors.Wrapf(err, "Error finding StateInfo for cluster: %v, %v", clusterProvider, cluster) + } + switch s.State { + case state.StateEnum.Approved: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Approved) + case state.StateEnum.Terminated: + return nil + case state.StateEnum.Created: + return pkgerrors.Wrap(err, "Cluster network intents have not been applied: "+cluster) + case state.StateEnum.Applied: + break + case state.StateEnum.Instantiated: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+state.StateEnum.Instantiated) + default: + return pkgerrors.Wrap(err, "Cluster is in an invalid state: "+cluster+" "+s.State) } // call resource synchronizer to terminate the CRs in the cluster - err = installappclient.InvokeUninstallApp(ctxVal) + err = installappclient.InvokeUninstallApp(s.ContextId) if err != nil { return err } // remove the app context - cleanuperr := context.DeleteCompositeApp() - if cleanuperr != nil { - log.Warn("Error deleted AppContext", log.Fields{ - "cluster-provider": clusterProvider, - "cluster": cluster, - }) + context, err := state.GetAppContextFromStateInfo(s) + if err != nil { + return pkgerrors.Wrap(err, "Error getting appcontext from cluster StateInfo : "+clusterProvider+" "+cluster) + } + err = context.DeleteCompositeApp() + if err != nil { + return pkgerrors.Wrap(err, "Error deleting appcontext of cluster : "+clusterProvider+" "+cluster) } - // remove the app context field from the cluster db record + // update StateInfo key := clusterPkg.ClusterKey{ ClusterProviderName: clusterProvider, ClusterName: cluster, } - err = db.DBconn.RemoveTag(v.db.StoreName, key, v.db.TagContext) + stateInfo := state.StateInfo{ + State: state.StateEnum.Terminated, + ContextId: "", + } + + err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagState, stateInfo) if err != nil { - log.Warn("Error removing AppContext from Cluster document", log.Fields{ - "cluster-provider": clusterProvider, - "cluster": cluster, - }) + return pkgerrors.Wrap(err, "Error updating the stateInfo of cluster: "+cluster) } + return nil } diff --git a/src/orchestrator/api/api.go b/src/orchestrator/api/api.go index 097b9ab3..72b444b7 100644 --- a/src/orchestrator/api/api.go +++ b/src/orchestrator/api/api.go @@ -56,6 +56,7 @@ func NewRouter(projectClient moduleLib.ProjectManager, client: ControllerClient, } router.HandleFunc("/projects", projHandler.createHandler).Methods("POST") + router.HandleFunc("/projects/{project-name}", projHandler.updateHandler).Methods("PUT") router.HandleFunc("/projects/{project-name}", projHandler.getHandler).Methods("GET") router.HandleFunc("/projects", projHandler.getHandler).Methods("GET") router.HandleFunc("/projects/{project-name}", projHandler.deleteHandler).Methods("DELETE") @@ -188,6 +189,8 @@ func NewRouter(projectClient moduleLib.ProjectManager, client: instantiationClient, } + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/approve", instantiationHandler.approveHandler).Methods("POST") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/terminate", instantiationHandler.terminateHandler).Methods("POST") router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/instantiate", instantiationHandler.instantiateHandler).Methods("POST") router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/terminate", instantiationHandler.terminateHandler).Methods("POST") router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/status", instantiationHandler.statusHandler).Methods("GET") diff --git a/src/orchestrator/api/instantiation_handler.go b/src/orchestrator/api/instantiation_handler.go index ce50e5b8..eeac8a00 100644 --- a/src/orchestrator/api/instantiation_handler.go +++ b/src/orchestrator/api/instantiation_handler.go @@ -31,6 +31,23 @@ type instantiationHandler struct { client moduleLib.InstantiationManager } +func (h instantiationHandler) approveHandler(w http.ResponseWriter, r *http.Request) { + + vars := mux.Vars(r) + p := vars["project-name"] + ca := vars["composite-app-name"] + v := vars["composite-app-version"] + di := vars["deployment-intent-group-name"] + + iErr := h.client.Approve(p, ca, v, di) + if iErr != nil { + http.Error(w, iErr.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusAccepted) + +} + func (h instantiationHandler) instantiateHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/src/orchestrator/api/projecthandler.go b/src/orchestrator/api/projecthandler.go index 09b24096..83211b64 100644 --- a/src/orchestrator/api/projecthandler.go +++ b/src/orchestrator/api/projecthandler.go @@ -54,7 +54,7 @@ func (h projectHandler) createHandler(w http.ResponseWriter, r *http.Request) { return } - ret, err := h.client.CreateProject(p) + ret, err := h.client.CreateProject(p, false) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -69,6 +69,50 @@ func (h projectHandler) createHandler(w http.ResponseWriter, r *http.Request) { } } +// Update handles updating the Project entry in the database +func (h projectHandler) updateHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + name := vars["project-name"] + + var p moduleLib.Project + + err := json.NewDecoder(r.Body).Decode(&p) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Name is required. + if p.MetaData.Name == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + // Name in URL should match name in body + if p.MetaData.Name != name { + http.Error(w, "Mismatched name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateProject(p, true) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // Get handles GET operations on a particular Project Name // Returns a Project func (h projectHandler) getHandler(w http.ResponseWriter, r *http.Request) { diff --git a/src/orchestrator/api/projecthandler_test.go b/src/orchestrator/api/projecthandler_test.go index dae87e2b..6810099f 100644 --- a/src/orchestrator/api/projecthandler_test.go +++ b/src/orchestrator/api/projecthandler_test.go @@ -40,7 +40,7 @@ type mockProjectManager struct { Err error } -func (m *mockProjectManager) CreateProject(inp moduleLib.Project) (moduleLib.Project, error) { +func (m *mockProjectManager) CreateProject(inp moduleLib.Project, exists bool) (moduleLib.Project, error) { if m.Err != nil { return moduleLib.Project{}, m.Err } @@ -144,6 +144,99 @@ func TestProjectCreateHandler(t *testing.T) { } } +func TestProjectUpdateHandler(t *testing.T) { + testCases := []struct { + label, name string + reader io.Reader + expected moduleLib.Project + expectedCode int + projectClient *mockProjectManager + }{ + { + label: "Missing Project Name in Request Body", + name: "testProject", + reader: bytes.NewBuffer([]byte(`{ + "description":"test description" + }`)), + expectedCode: http.StatusBadRequest, + projectClient: &mockProjectManager{}, + }, + { + label: "Missing Body Failure", + name: "testProject", + expectedCode: http.StatusBadRequest, + projectClient: &mockProjectManager{}, + }, + { + label: "Mismatched Name Failure", + name: "testProject", + expectedCode: http.StatusBadRequest, + reader: bytes.NewBuffer([]byte(`{ + "metadata" : { + "name": "testProjectNameMismatch", + "description": "Test Project used for unit testing" + } + }`)), + projectClient: &mockProjectManager{}, + }, + { + label: "Update Project", + name: "testProject", + expectedCode: http.StatusOK, + reader: bytes.NewBuffer([]byte(`{ + "metadata" : { + "name": "testProject", + "description": "Test Project used for unit testing" + } + }`)), + expected: moduleLib.Project{ + MetaData: moduleLib.ProjectMetaData{ + Name: "testProject", + Description: "Test Project used for unit testing", + UserData1: "update data1", + UserData2: "update data2", + }, + }, + projectClient: &mockProjectManager{ + //Items that will be returned by the mocked Client + Items: []moduleLib.Project{ + { + MetaData: moduleLib.ProjectMetaData{ + Name: "testProject", + Description: "Test Project used for unit testing", + UserData1: "update data1", + UserData2: "update data2", + }, + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("PUT", "/v2/projects/"+testCase.name, testCase.reader) + resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := moduleLib.Project{} + json.NewDecoder(resp.Body).Decode(&got) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("updateHandler returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestProjectGetHandler(t *testing.T) { testCases := []struct { diff --git a/src/orchestrator/examples/example_module.go b/src/orchestrator/examples/example_module.go index 9138b085..7edbb758 100644 --- a/src/orchestrator/examples/example_module.go +++ b/src/orchestrator/examples/example_module.go @@ -31,7 +31,14 @@ func ExampleClient_Project() { return } // Perform operations on Project Module - _, err := c.Project.CreateProject(moduleLib.Project{MetaData: moduleLib.ProjectMetaData{Name: "test", Description: "test", UserData1: "userData1", UserData2: "userData2"}}) + // POST request (exists == false) + _, err := c.Project.CreateProject(moduleLib.Project{MetaData: moduleLib.ProjectMetaData{Name: "test", Description: "test", UserData1: "userData1", UserData2: "userData2"}}, false) + if err != nil { + log.Println(err) + return + } + // PUT request (exists == true) + _, err = c.Project.CreateProject(moduleLib.Project{MetaData: moduleLib.ProjectMetaData{Name: "test", Description: "test", UserData1: "userData1", UserData2: "userData2"}}, true) if err != nil { log.Println(err) return diff --git a/src/orchestrator/go.mod b/src/orchestrator/go.mod index 22e1aba1..f05343cd 100644 --- a/src/orchestrator/go.mod +++ b/src/orchestrator/go.mod @@ -16,7 +16,6 @@ require ( github.com/lib/pq v1.6.0 // indirect github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect github.com/mitchellh/copystructure v1.0.0 // indirect - github.com/onap/multicloud-k8s/src/clm v0.0.0-00010101000000-000000000000 github.com/onap/multicloud-k8s/src/monitor v0.0.0-20200630152613-7c20f73e7c5d github.com/onap/multicloud-k8s/src/ncm v0.0.0-20200515060444-c77850a75eee github.com/onap/multicloud-k8s/src/rsync v0.0.0-20200630152613-7c20f73e7c5d diff --git a/src/orchestrator/pkg/module/deployment_intent_groups.go b/src/orchestrator/pkg/module/deployment_intent_groups.go index c7237032..d017c1e9 100644 --- a/src/orchestrator/pkg/module/deployment_intent_groups.go +++ b/src/orchestrator/pkg/module/deployment_intent_groups.go @@ -20,8 +20,8 @@ import ( "encoding/json" "reflect" - appcontext "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -62,7 +62,7 @@ type OverrideValues struct { type DeploymentIntentGroupManager interface { CreateDeploymentIntentGroup(d DeploymentIntentGroup, p string, ca string, v string) (DeploymentIntentGroup, error) GetDeploymentIntentGroup(di string, p string, ca string, v string) (DeploymentIntentGroup, error) - GetDeploymentIntentGroupContext(di string, p string, ca string, v string) (appcontext.AppContext, string, error) + GetDeploymentIntentGroupState(di string, p string, ca string, v string) (state.StateInfo, error) DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error GetAllDeploymentIntentGroups(p string, ca string, v string) ([]DeploymentIntentGroup, error) } @@ -89,7 +89,7 @@ func (dk DeploymentIntentGroupKey) String() string { type DeploymentIntentGroupClient struct { storeName string tagMetaData string - tagContext string + tagState string } // NewDeploymentIntentGroupClient return an instance of DeploymentIntentGroupClient which implements DeploymentIntentGroupManager @@ -97,7 +97,7 @@ func NewDeploymentIntentGroupClient() *DeploymentIntentGroupClient { return &DeploymentIntentGroupClient{ storeName: "orchestrator", tagMetaData: "deploymentintentgroupmetadata", - tagContext: "contextid", + tagState: "stateInfo", } } @@ -134,6 +134,17 @@ func (c *DeploymentIntentGroupClient) CreateDeploymentIntentGroup(d DeploymentIn return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Create DB entry error") } + // Add the stateInfo record + stateInfo := state.StateInfo{ + State: state.StateEnum.Created, + ContextId: "", + } + + err = db.DBconn.Insert(c.storeName, gkey, nil, c.tagState, stateInfo) + if err != nil { + return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Error updating the stateInfo of the DeploymentIntentGroup: "+d.MetaData.Name) + } + return d, nil } @@ -205,8 +216,8 @@ func (c *DeploymentIntentGroupClient) GetAllDeploymentIntentGroups(p string, ca } -// GetDeploymentIntentGroupContext returns the AppContent with a given DeploymentIntentname, project, compositeAppName and version of compositeApp -func (c *DeploymentIntentGroupClient) GetDeploymentIntentGroupContext(di string, p string, ca string, v string) (appcontext.AppContext, string, error) { +// GetDeploymentIntentGroupState returns the AppContent with a given DeploymentIntentname, project, compositeAppName and version of compositeApp +func (c *DeploymentIntentGroupClient) GetDeploymentIntentGroupState(di string, p string, ca string, v string) (state.StateInfo, error) { key := DeploymentIntentGroupKey{ Name: di, @@ -215,22 +226,21 @@ func (c *DeploymentIntentGroupClient) GetDeploymentIntentGroupContext(di string, Version: v, } - result, err := db.DBconn.Find(c.storeName, key, c.tagContext) + result, err := db.DBconn.Find(c.storeName, key, c.tagState) if err != nil { - return appcontext.AppContext{}, "", pkgerrors.Wrap(err, "Get DeploymentIntentGroup Context error") + return state.StateInfo{}, pkgerrors.Wrap(err, "Get DeploymentIntentGroup StateInfo error") } if result != nil { - ctxVal := string(result[0]) - var cc appcontext.AppContext - _, err = cc.LoadAppContext(ctxVal) + s := state.StateInfo{} + err = db.DBconn.Unmarshal(result[0], &s) if err != nil { - return appcontext.AppContext{}, "", pkgerrors.Wrap(err, "Error loading DeploymentIntentGroup Appcontext") + return state.StateInfo{}, pkgerrors.Wrap(err, "Unmarshalling DeploymentIntentGroup StateInfo") } - return cc, ctxVal, nil + return s, nil } - return appcontext.AppContext{}, "", pkgerrors.New("Error getting DeploymentIntentGroup AppContext") + return state.StateInfo{}, pkgerrors.New("Error getting DeploymentIntentGroup StateInfo") } // DeleteDeploymentIntentGroup deletes a DeploymentIntentGroup @@ -241,9 +251,9 @@ func (c *DeploymentIntentGroupClient) DeleteDeploymentIntentGroup(di string, p s CompositeApp: ca, Version: v, } - _, _, err := c.GetDeploymentIntentGroupContext(di, p, ca, v) - if err == nil { - return pkgerrors.Wrap(err, "DeploymentIntentGroup must be terminated before it can be deleted "+di) + s, err := c.GetDeploymentIntentGroupState(di, p, ca, v) + if err == nil && s.State == state.StateEnum.Instantiated { + return pkgerrors.Errorf("DeploymentIntentGroup must be terminated before it can be deleted " + di) } err = db.DBconn.Remove(c.storeName, k) diff --git a/src/orchestrator/pkg/module/instantiation.go b/src/orchestrator/pkg/module/instantiation.go index f4e75861..9c0c9e31 100644 --- a/src/orchestrator/pkg/module/instantiation.go +++ b/src/orchestrator/pkg/module/instantiation.go @@ -25,6 +25,7 @@ import ( gpic "github.com/onap/multicloud-k8s/src/orchestrator/pkg/gpic" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" "github.com/onap/multicloud-k8s/src/orchestrator/utils/helm" pkgerrors "github.com/pkg/errors" ) @@ -71,32 +72,67 @@ type InstantiationKey struct { // InstantiationManager is an interface which exposes the // InstantiationManager functionalities type InstantiationManager interface { - //ApproveInstantiation(p string, ca string, v string, di string) (error) + Approve(p string, ca string, v string, di string) error Instantiate(p string, ca string, v string, di string) error Status(p string, ca string, v string, di string) (StatusData, error) Terminate(p string, ca string, v string, di string) error } -// InstantiationClientDbInfo consists of storeName and tagContext +// InstantiationClientDbInfo consists of storeName and tagState type InstantiationClientDbInfo struct { - storeName string // name of the mongodb collection to use for Instantiationclient documents - tagContext string // attribute key name for context object in App Context + storeName string // name of the mongodb collection to use for Instantiationclient documents + tagState string // attribute key name for context object in App Context } // NewInstantiationClient returns an instance of InstantiationClient func NewInstantiationClient() *InstantiationClient { return &InstantiationClient{ db: InstantiationClientDbInfo{ - storeName: "orchestrator", - tagContext: "contextid", + storeName: "orchestrator", + tagState: "stateInfo", }, } } -// TODO -//ApproveInstantiation approves an instantiation -// func (c InstantiationClient) ApproveInstantiation(p string, ca string, v string, di string) (error){ -// } +//Approve approves an instantiation +func (c InstantiationClient) Approve(p string, ca string, v string, di string) error { + s, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroupState(di, p, ca, v) + if err != nil { + return pkgerrors.Wrap(err, "DeploymentIntentGroup has no state info: "+di) + } + switch s.State { + case state.StateEnum.Approved: + return nil + case state.StateEnum.Terminated: + break + case state.StateEnum.Created: + break + case state.StateEnum.Applied: + return pkgerrors.Errorf("DeploymentIntentGroup is in an invalid state" + s.State) + case state.StateEnum.Instantiated: + return pkgerrors.Errorf("DeploymentIntentGroup has already been instantiated" + di) + default: + return pkgerrors.Errorf("DeploymentIntentGroup is in an unknown state" + s.State) + } + + key := DeploymentIntentGroupKey{ + Name: di, + Project: p, + CompositeApp: ca, + Version: v, + } + stateInfo := state.StateInfo{ + State: state.StateEnum.Approved, + ContextId: "", + } + + err = db.DBconn.Insert(c.db.storeName, key, nil, c.db.tagState, stateInfo) + if err != nil { + return pkgerrors.Wrap(err, "Error updating the stateInfo of the DeploymentIntentGroup: "+di) + } + + return nil +} func getOverrideValuesByAppName(ov []OverrideValues, a string) map[string]string { for _, eachOverrideVal := range ov { @@ -189,9 +225,23 @@ func (c InstantiationClient) Instantiate(p string, ca string, v string, di strin return pkgerrors.Wrap(err, "Not finding the deploymentIntentGroup") } - _, _, err = NewDeploymentIntentGroupClient().GetDeploymentIntentGroupContext(di, p, ca, v) - if err == nil { - return pkgerrors.Errorf("DeploymentIntentGroup has already been instantiated: " + di) + s, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroupState(di, p, ca, v) + if err != nil { + return pkgerrors.Errorf("Error retrieving DeploymentIntentGroup stateInfo: " + di) + } + switch s.State { + case state.StateEnum.Approved: + break + case state.StateEnum.Terminated: + break // TODO - ideally, should check that all resources have completed being terminated + case state.StateEnum.Created: + return pkgerrors.Errorf("DeploymentIntentGroup must be Approved before instantiating" + di) + case state.StateEnum.Applied: + return pkgerrors.Errorf("DeploymentIntentGroup is in an invalid state" + di) + case state.StateEnum.Instantiated: + return pkgerrors.Errorf("DeploymentIntentGroup has already been instantiated" + di) + default: + return pkgerrors.Errorf("DeploymentIntentGroup is in an unknown state" + s.State) } rName := dIGrp.Spec.Version //rName is releaseName @@ -292,8 +342,11 @@ func (c InstantiationClient) Instantiate(p string, ca string, v string, di strin CompositeApp: ca, Version: v, } - - err = db.DBconn.Insert(c.db.storeName, key, nil, c.db.tagContext, ctxval) + stateInfo := state.StateInfo{ + State: state.StateEnum.Instantiated, + ContextId: ctxval.(string), + } + err = db.DBconn.Insert(c.db.storeName, key, nil, c.db.tagState, stateInfo) if err != nil { cleanuperr := context.DeleteCompositeApp() if cleanuperr != nil { @@ -348,11 +401,16 @@ the deployment, which is made available in the appcontext. */ func (c InstantiationClient) Status(p string, ca string, v string, di string) (StatusData, error) { - ac, _, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroupContext(di, p, ca, v) + s, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroupState(di, p, ca, v) if err != nil { return StatusData{}, pkgerrors.Wrap(err, "deploymentIntentGroup not found: "+di) } + ac, err := state.GetAppContextFromStateInfo(s) + if err != nil { + return StatusData{}, pkgerrors.Wrap(err, "AppContext for deploymentIntentGroup not found: "+di) + } + // Get all apps in this composite app allApps, err := NewAppClient().GetApps(p, ca, v) if err != nil { @@ -409,12 +467,19 @@ DeploymentIntentName and calls rsync to terminate. */ func (c InstantiationClient) Terminate(p string, ca string, v string, di string) error { - ac, ctxval, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroupContext(di, p, ca, v) + s, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroupState(di, p, ca, v) + if err != nil { + return pkgerrors.Wrap(err, "DeploymentIntentGroup has no state info: "+di) + } else if s.State != state.StateEnum.Instantiated { + return pkgerrors.Errorf("DeploymentIntentGroup is not instantiated" + di) + } + + ac, err := state.GetAppContextFromStateInfo(s) if err != nil { - return pkgerrors.Wrap(err, "DeploymentIntentGroup has no app context: "+di) + return pkgerrors.Wrap(err, "AppContext for deploymentIntentGroup not found: "+di) } - err = callRsyncUninstall(ctxval) + err = callRsyncUninstall(s.ContextId) if err != nil { return err } @@ -430,10 +495,14 @@ func (c InstantiationClient) Terminate(p string, ca string, v string, di string) CompositeApp: ca, Version: v, } + stateInfo := state.StateInfo{ + State: state.StateEnum.Terminated, + ContextId: "", + } - err = db.DBconn.RemoveTag(c.db.storeName, key, c.db.tagContext) + err = db.DBconn.Insert(c.db.storeName, key, nil, c.db.tagState, stateInfo) if err != nil { - return pkgerrors.Wrap(err, "Error removing the app context tag from DeploymentIntentGroup: "+di) + return pkgerrors.Wrap(err, "Error updating the stateInfo of the DeploymentIntentGroup: "+di) } return nil diff --git a/src/orchestrator/pkg/module/project.go b/src/orchestrator/pkg/module/project.go index 02f6d827..e86266b9 100644 --- a/src/orchestrator/pkg/module/project.go +++ b/src/orchestrator/pkg/module/project.go @@ -55,7 +55,7 @@ func (pk ProjectKey) String() string { // ProjectManager is an interface exposes the Project functionality type ProjectManager interface { - CreateProject(pr Project) (Project, error) + CreateProject(pr Project, exists bool) (Project, error) GetProject(name string) (Project, error) DeleteProject(name string) error GetAllProjects() ([]Project, error) @@ -78,7 +78,7 @@ func NewProjectClient() *ProjectClient { } // CreateProject a new collection based on the project -func (v *ProjectClient) CreateProject(p Project) (Project, error) { +func (v *ProjectClient) CreateProject(p Project, exists bool) (Project, error) { //Construct the composite key to select the entry key := ProjectKey{ @@ -87,7 +87,7 @@ func (v *ProjectClient) CreateProject(p Project) (Project, error) { //Check if this Project already exists _, err := v.GetProject(p.MetaData.Name) - if err == nil { + if err == nil && !exists { return Project{}, pkgerrors.New("Project already exists") } diff --git a/src/orchestrator/pkg/module/project_test.go b/src/orchestrator/pkg/module/project_test.go index f6856f86..947478b4 100644 --- a/src/orchestrator/pkg/module/project_test.go +++ b/src/orchestrator/pkg/module/project_test.go @@ -62,13 +62,39 @@ func TestCreateProject(t *testing.T) { Err: pkgerrors.New("Error Creating Project"), }, }, + { + label: "Create Existing Project", + inp: Project{ + MetaData: ProjectMetaData{ + Name: "testProject", + Description: "A sample Project used for unit testing", + UserData1: "data1", + UserData2: "data2", + }, + }, + expectedError: "Project already exists", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProjectKey{ProjectName: "testProject"}.String(): { + "projectmetadata": []byte( + "{" + + "\"metadata\" : {" + + "\"Name\":\"testProject\"," + + "\"Description\":\"Test project for unit testing\"," + + "\"UserData1\":\"userData1\"," + + "\"UserData2\":\"userData2\"}" + + "}"), + }, + }, + }, + }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProjectClient() - got, err := impl.CreateProject(testCase.inp) + got, err := impl.CreateProject(testCase.inp, false) if err != nil { if testCase.expectedError == "" { t.Fatalf("Create returned an unexpected error %s", err) @@ -86,6 +112,85 @@ func TestCreateProject(t *testing.T) { } } +func TestUpdateProject(t *testing.T) { + testCases := []struct { + label string + inp Project + expectedError string + mockdb *db.MockDB + expected Project + }{ + { + label: "Update Project", + inp: Project{ + MetaData: ProjectMetaData{ + Name: "testProject", + Description: "Test project for unit testing", + UserData1: "update userData1", + UserData2: "update userData2", + }, + }, + expected: Project{ + MetaData: ProjectMetaData{ + Name: "testProject", + Description: "Test project for unit testing", + UserData1: "update userData1", + UserData2: "update userData2", + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProjectKey{ProjectName: "testProject"}.String(): { + "projectmetadata": []byte( + "{" + + "\"metadata\" : {" + + "\"Name\":\"testProject\"," + + "\"Description\":\"Test project for unit testing\"," + + "\"UserData1\":\"userData1\"," + + "\"UserData2\":\"userData2\"}" + + "}"), + }, + }, + }, + }, + { + label: "Failed Update Project", + inp: Project{ + MetaData: ProjectMetaData{ + Name: "unknownProject", + Description: "Unknown project for unit testing", + }, + }, + expectedError: "Creating DB Entry", + mockdb: &db.MockDB{ + Err: pkgerrors.New("Error Updating Project"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewProjectClient() + got, err := impl.CreateProject(testCase.inp, true) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Update returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Update returned an unexpected error %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("Update returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestGetProject(t *testing.T) { testCases := []struct { diff --git a/src/orchestrator/pkg/state/state_helper.go b/src/orchestrator/pkg/state/state_helper.go new file mode 100644 index 00000000..a65cea8d --- /dev/null +++ b/src/orchestrator/pkg/state/state_helper.go @@ -0,0 +1,29 @@ +/* + * Copyright 2020 Intel Corporation, Inc + * + * 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 state + +import "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" + +// GetAppContextFromStateInfo loads the appcontext present in the StateInfo input +func GetAppContextFromStateInfo(s StateInfo) (appcontext.AppContext, error) { + var cc appcontext.AppContext + _, err := cc.LoadAppContext(s.ContextId) + if err != nil { + return appcontext.AppContext{}, err + } + return cc, nil +} diff --git a/src/orchestrator/pkg/state/types.go b/src/orchestrator/pkg/state/types.go new file mode 100644 index 00000000..25fb60d2 --- /dev/null +++ b/src/orchestrator/pkg/state/types.go @@ -0,0 +1,42 @@ +/* + * Copyright 2020 Intel Corporation, Inc + * + * 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 state + +// StateInfo struct is used to maintain the values for state, contextid, (and other) +// information about resources which can be instantiated via rsync. +type StateInfo struct { + State StateValue + ContextId string +} + +type StateValue = string + +type states struct { + Created StateValue + Approved StateValue + Applied StateValue + Instantiated StateValue + Terminated StateValue +} + +var StateEnum = &states{ + Created: "Created", + Approved: "Approved", + Applied: "Applied", + Instantiated: "Instantiated", + Terminated: "Terminated", +} |