| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | |
| garciadeblas | ea4ef74 | 2024-09-16 09:37:20 +0200 | [diff] [blame^] | 19 | import base64 |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 20 | import json |
| garciadeblas | ea4ef74 | 2024-09-16 09:37:20 +0200 | [diff] [blame^] | 21 | import yaml |
| 22 | from osm_lcm.lcm_utils import LcmException |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 23 | |
| 24 | |
| 25 | async def create_cloud_credentials(self, op_id, op_params, content): |
| 26 | self.logger.info("Create cloud_credentials workflow Enter") |
| 27 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 28 | |
| 29 | workflow_template = "launcher-create-providerconfig.j2" |
| 30 | workflow_name = f"create-providerconfig-{content['_id']}" |
| 31 | # vim_name = content["name"].lower() |
| 32 | vim_name = content.get("git_name", content["name"]).lower() |
| 33 | # workflow_name = f"{op_id}-create-credentials-{vim_name}" |
| 34 | |
| 35 | # Test kubectl connection |
| 36 | self.logger.debug(self._kubectl._get_kubectl_version()) |
| 37 | |
| 38 | # Create secret with creds |
| 39 | secret_name = workflow_name |
| 40 | secret_namespace = "osm-workflows" |
| 41 | secret_key = "creds" |
| garciadeblas | ea4ef74 | 2024-09-16 09:37:20 +0200 | [diff] [blame^] | 42 | cloud_config = content.get("config", {}) |
| 43 | if "credentials_base64" in cloud_config: |
| 44 | secret_value = base64.b64decode(cloud_config["credentials_base64"]).decode( |
| 45 | "utf-8" |
| 46 | ) |
| 47 | elif "credentials" in cloud_config: |
| 48 | secret_value = json.dumps(cloud_config["credentials"], indent=2) |
| 49 | else: |
| 50 | raise LcmException("No credentials in VIM/cloud config") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 51 | await self.create_secret( |
| 52 | secret_name, |
| 53 | secret_namespace, |
| 54 | secret_key, |
| 55 | secret_value, |
| 56 | ) |
| 57 | |
| 58 | # Additional params for the workflow |
| 59 | providerconfig_name = f"{vim_name}-config" |
| 60 | provider_type = content["vim_type"] |
| 61 | osm_project_name = "osm_admin" # TODO: get project name from content |
| 62 | if provider_type == "gcp": |
| 63 | vim_tenant = content["vim_tenant_name"] |
| 64 | else: |
| 65 | vim_tenant = "" |
| 66 | |
| 67 | # Render workflow |
| 68 | manifest = self.render_jinja_template( |
| 69 | workflow_template, |
| 70 | output_file=None, |
| 71 | workflow_name=workflow_name, |
| 72 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 73 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 74 | providerconfig_name=providerconfig_name, |
| 75 | provider_type=provider_type, |
| 76 | cred_secret_name=vim_name, |
| 77 | temp_cred_secret_name=secret_name, |
| 78 | public_key_mgmt=self._pubkey, |
| 79 | osm_project_name=osm_project_name, |
| 80 | target_gcp_project=vim_tenant, |
| 81 | workflow_debug=self._workflow_debug, |
| 82 | workflow_dry_run=self._workflow_dry_run, |
| 83 | ) |
| 84 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 85 | |
| 86 | # Submit workflow |
| 87 | self._kubectl.create_generic_object( |
| 88 | namespace="osm-workflows", |
| 89 | manifest_dict=yaml.safe_load(manifest), |
| 90 | api_group="argoproj.io", |
| 91 | api_plural="workflows", |
| 92 | api_version="v1alpha1", |
| 93 | ) |
| 94 | return workflow_name |
| 95 | |
| 96 | |
| 97 | async def delete_cloud_credentials(self, op_id, op_params, content): |
| 98 | self.logger.info("Delete cloud_credentials workflow Enter") |
| 99 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 100 | |
| 101 | workflow_template = "launcher-delete-providerconfig.j2" |
| 102 | workflow_name = f"delete-providerconfig-{content['_id']}" |
| 103 | # vim_name = content["name"].lower() |
| 104 | vim_name = content.get("git_name", content["name"]).lower() |
| 105 | # workflow_name = f"{op_id}-delete-credentials-{vim_name}" |
| 106 | |
| 107 | # Additional params for the workflow |
| 108 | providerconfig_name = f"{vim_name}-config" |
| 109 | provider_type = content["vim_type"] |
| 110 | osm_project_name = "osm_admin" # TODO: get project name from content |
| 111 | |
| 112 | # Render workflow |
| 113 | manifest = self.render_jinja_template( |
| 114 | workflow_template, |
| 115 | output_file=None, |
| 116 | workflow_name=workflow_name, |
| 117 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 118 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 119 | providerconfig_name=providerconfig_name, |
| 120 | provider_type=provider_type, |
| 121 | osm_project_name=osm_project_name, |
| 122 | workflow_debug=self._workflow_debug, |
| 123 | workflow_dry_run=self._workflow_dry_run, |
| 124 | ) |
| 125 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 126 | |
| 127 | # Submit workflow |
| 128 | self._kubectl.create_generic_object( |
| 129 | namespace="osm-workflows", |
| 130 | manifest_dict=yaml.safe_load(manifest), |
| 131 | api_group="argoproj.io", |
| 132 | api_plural="workflows", |
| 133 | api_version="v1alpha1", |
| 134 | ) |
| 135 | return workflow_name |
| 136 | |
| 137 | |
| 138 | async def update_cloud_credentials(self, op_id, op_params, content): |
| 139 | self.logger.info("Update cloud_credentials workflow Enter") |
| 140 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 141 | |
| 142 | workflow_template = "launcher-update-providerconfig.j2" |
| 143 | workflow_name = f"update-providerconfig-{content['_id']}" |
| 144 | # vim_name = content["name"].lower() |
| 145 | vim_name = content.get("git_name", content["name"]).lower() |
| 146 | # workflow_name = f"{op_id}-update-credentials-{vim_name}" |
| 147 | |
| 148 | # Create secret with creds |
| 149 | secret_name = workflow_name |
| 150 | secret_namespace = "osm-workflows" |
| 151 | secret_key = "creds" |
| garciadeblas | ea4ef74 | 2024-09-16 09:37:20 +0200 | [diff] [blame^] | 152 | cloud_config = content.get("config", {}) |
| 153 | if "credentials_base64" in cloud_config: |
| 154 | secret_value = base64.b64decode(cloud_config["credentials_base64"]).decode( |
| 155 | "utf-8" |
| 156 | ) |
| 157 | elif "credentials" in cloud_config: |
| 158 | secret_value = json.dumps(cloud_config["credentials"], indent=2) |
| 159 | else: |
| 160 | raise LcmException("No credentials in VIM/cloud config") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 161 | await self.create_secret( |
| 162 | secret_name, |
| 163 | secret_namespace, |
| 164 | secret_key, |
| 165 | secret_value, |
| 166 | ) |
| 167 | # Additional params for the workflow |
| 168 | providerconfig_name = f"{vim_name}-config" |
| 169 | provider_type = content["vim_type"] |
| 170 | osm_project_name = "osm_admin" # TODO: get project name from content |
| 171 | if provider_type == "gcp": |
| 172 | vim_tenant = content["vim_tenant_name"] |
| 173 | else: |
| 174 | vim_tenant = "" |
| 175 | |
| 176 | # Render workflow |
| 177 | manifest = self.render_jinja_template( |
| 178 | workflow_template, |
| 179 | output_file=None, |
| 180 | workflow_name=workflow_name, |
| 181 | git_fleet_url=f"{self._repo_base_url}/{self._repo_user}/fleet-osm.git", |
| 182 | git_sw_catalogs_url=f"{self._repo_base_url}/{self._repo_user}/sw-catalogs-osm.git", |
| 183 | providerconfig_name=providerconfig_name, |
| 184 | provider_type=provider_type, |
| 185 | cred_secret_name=vim_name, |
| 186 | temp_cred_secret_name=secret_name, |
| 187 | public_key_mgmt=self._pubkey, |
| 188 | osm_project_name=osm_project_name, |
| 189 | target_gcp_project=vim_tenant, |
| 190 | workflow_debug=self._workflow_debug, |
| 191 | workflow_dry_run=self._workflow_dry_run, |
| 192 | ) |
| 193 | self.logger.debug(f"Workflow manifest: {manifest}") |
| 194 | |
| 195 | # Submit workflow |
| 196 | self._kubectl.create_generic_object( |
| 197 | namespace="osm-workflows", |
| 198 | manifest_dict=yaml.safe_load(manifest), |
| 199 | api_group="argoproj.io", |
| 200 | api_plural="workflows", |
| 201 | api_version="v1alpha1", |
| 202 | ) |
| 203 | return workflow_name |
| 204 | |
| 205 | |
| 206 | async def check_create_cloud_credentials(self, op_id, op_params, content): |
| 207 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 208 | return True, "OK" |
| 209 | |
| 210 | |
| 211 | async def check_update_cloud_credentials(self, op_id, op_params, content): |
| 212 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 213 | return True, "OK" |
| 214 | |
| 215 | |
| 216 | async def check_delete_cloud_credentials(self, op_id, op_params, content): |
| 217 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 218 | return True, "OK" |