| garciadeblas | 83775ba | 2025-07-23 18:35:24 +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 | # Helper module to manage KSU source or target locations, so that they can be safely translated to well-known paths in the local filesystem. |
| 19 | |
| 20 | |
| 21 | # Helper function to convert a profile type name to the canonical name for that profile type so that it can build a folder path deterministically. |
| 22 | # NOT EXPORTED |
| 23 | def "normalize profile type" [ |
| 24 | ]: [ |
| 25 | string -> string |
| 26 | ] { |
| 27 | $in |
| 28 | | if $in in ["controller", "infra-controller", "infra-controllers", "infra_controller", "infra_controllers"] { |
| 29 | "infra-controller-profiles" |
| 30 | } else if $in in ["config", "infra-config", "infra-configs", "infra_config", "infra_configs"] { |
| 31 | "infra-config-profiles" |
| 32 | } else if $in in ["managed", "resources", "managed-resources", "managed_resources"] { |
| 33 | "managed-resources" |
| 34 | } else if $in in ["app", "apps", "applications", "cnf", "cnfs", "nf", "nfs"] { |
| 35 | "app-profiles" |
| 36 | } else { |
| 37 | $in |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | |
| 42 | # Helper function to convert an OKA type name to the canonical name for that OKA type so that it can build a folder path deterministically. |
| 43 | # NOT EXPORTED |
| 44 | def "normalize oka type" [ |
| 45 | ]: [ |
| 46 | string -> string |
| 47 | ] { |
| 48 | $in |
| 49 | | if $in in ["controller", "infra-controller", "infra-controllers", "infra_controller", "infra_controllers"] { |
| 50 | "infra-controllers" |
| 51 | } else if $in in ["config", "infra-config", "infra-configs", "infra_config", "infra_configs"] { |
| 52 | "infra-configs" |
| 53 | } else if $in in ["managed", "resources", "managed-resources", "managed_resources", "cloud-resources", "cloud_resources"] { |
| 54 | "cloud-resources" |
| 55 | } else if $in in ["app", "apps", "applications", "cnf", "cnfs", "nf", "nfs"] { |
| 56 | "apps" |
| 57 | } else { |
| 58 | $in |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | # Convert a location into its components to determine a path in the local filesystem. |
| 64 | export def "to path components" [ |
| 65 | default_project_name: string = "osm_admin" # Default project name |
| 66 | default_repos_base: string = "/repos" # Base path for the local repo clones |
| 67 | ]: [ |
| 68 | record -> list<path> |
| 69 | ] { |
| 70 | let in_location: record = $in |
| 71 | |
| 72 | # Absolute path of the local repo clone |
| 73 | let repo: string = ( |
| 74 | $in_location |
| 75 | # Is it a path? |
| 76 | | if ($in | get -i "repo-path" | is-not-empty ) { |
| 77 | # $in_location |
| 78 | $in |
| 79 | | get "repo-path" |
| 80 | | path join |
| 81 | # Maybe it was specified by repo name? |
| 82 | } else if ( |
| 83 | ($in | get -i "repo-name" | is-not-empty ) |
| 84 | ) { |
| 85 | [ |
| 86 | # ($in_location | get -i "repos-base" | default $default_repos_base), |
| 87 | # ($in_location | get "repo-name") |
| 88 | ($in | get -i "repos-base" | default $default_repos_base), |
| 89 | ($in | get "repo-name") |
| 90 | ] |
| 91 | | path join |
| 92 | # Otherwise, throws an error |
| 93 | } else { |
| 94 | error make { msg: $"Error: Invalid location spec. Missing `repo-path` or `repo-name` key. Non conformant: \n($in_location | to yaml)"} |
| 95 | } |
| 96 | # Ensure that the absolute path starts by "/" |
| 97 | | if ($in | str starts-with "/") { |
| 98 | $in |
| 99 | } else { |
| 100 | $"/($in)" |
| 101 | } |
| 102 | ) |
| 103 | |
| 104 | # Get the base path prior to the last item (e.g., profile path or OKA folder) |
| 105 | let base: string = ( |
| 106 | $in_location |
| 107 | # Is it a path? |
| 108 | | if ($in | get -i "base-path" | is-not-empty ) { |
| 109 | $in |
| 110 | | get "base-path" |
| 111 | | path join |
| 112 | # Maybe it is a profile spec? |
| 113 | } else if ( |
| 114 | ($in | get -i "profile-type" | is-not-empty ) and |
| 115 | ($in | get -i "profile-name" | is-not-empty ) |
| 116 | ) { |
| 117 | [ |
| 118 | ($in | get -i "project-name" | default $default_project_name), |
| 119 | ($in | get "profile-type" | normalize profile type), |
| 120 | ($in | get "profile-name") |
| 121 | ] |
| 122 | | path join |
| 123 | # Maybe it is an OKA subfolder spec? |
| 124 | } else if ( |
| 125 | ($in | get -i "oka-type" | is-not-empty ) and |
| 126 | ($in | get -i "oka-name" | is-not-empty ) |
| 127 | ) { |
| 128 | [ |
| 129 | ($in | get "oka-type" | normalize oka type), |
| 130 | ($in | get "oka-name") |
| 131 | ] |
| 132 | | path join |
| 133 | # Otherwise, it is malformed |
| 134 | } else { |
| 135 | error make { msg: $"Error: Invalid location spec. Missing `base-path` or `profile-type`+`profile-name` or `oka-type`+`oka-name` key. Non conformant: \n($in | to yaml)"} |
| 136 | } |
| 137 | ) |
| 138 | |
| 139 | # Check that the final relative path is available |
| 140 | if ($in_location | get -i "relative-path" | is-empty ) { |
| 141 | error make { msg: $"Error: Invalid location spec. Missing `relative-path` key. Non conformant: \n($in_location | to yaml)"} |
| 142 | } |
| 143 | |
| 144 | # Finally, return the path components |
| 145 | [ $repo, $base, ($in_location | get "relative-path" | path join) ] |
| 146 | } |
| 147 | |
| 148 | |
| 149 | # Convert a location to an absolute path in the local filesystem. |
| 150 | export def "to absolute path" [ |
| 151 | default_project_name: string = "osm_admin" # Default project name |
| 152 | default_repos_base: string = "/repos" # Base path for the local repo clones |
| 153 | ]: [ |
| 154 | record -> path |
| 155 | ] { |
| 156 | $in |
| 157 | | to path components $default_project_name $default_repos_base |
| 158 | | path join |
| 159 | } |
| 160 | |
| 161 | |
| 162 | # Convert a location to a relative path in the local filesystem with respect to the root of the locally cloned repo. |
| 163 | export def "from base path" [ |
| 164 | default_project_name: string = "osm_admin" # Default project name |
| 165 | ]: [ |
| 166 | record -> path |
| 167 | ] { |
| 168 | $in |
| 169 | | to path components $default_project_name |
| 170 | # Drop the first item (the `repo-path`) |
| 171 | | skip 1 |
| 172 | | path join |
| 173 | } |