All files / modules/20-rbac/pages/ResourceGroupDetails utils.tsx

74.59% Statements 91/122
64.17% Branches 77/120
66.67% Functions 22/33
71.3% Lines 77/108

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300              11x   11x   11x                   11x   11x 11x   11x 11x 11x 11x     11x 11x 11x 11x     11x       4x 4x         4x 4x     4x         4x     11x 2x   3x 3x 1x     2x   1x     11x 2x 2x 3x 2x       1x           2x     11x             2x 1x 1x   1x                                     1x 1x   1x                         11x                                                 11x         24x 24x 48x 24x     11x 34x                   34x             11x 4x     4x   11x 28x 28x 28x 28x                   11x           1x                                 11x       4x 4x 12x           8x   12x       11x         4x 4x 4x     4x 4x     4x         4x     11x                                                    
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import produce from 'immer'
import type { SelectOption } from '@wings-software/uicore'
import { RbacResourceGroupTypes } from '@rbac/constants/utils'
import type { ResourceType } from '@rbac/interfaces/ResourceType'
import { isDynamicResourceSelector, SelectionType } from '@rbac/utils/utils'
import type {
  ResourceSelector,
  ResponseResourceTypeDTO,
  ScopeSelector,
  ResourceGroupV2,
  ResourceSelectorV2,
  ResourceGroupV2Request,
  ResourceFilter
} from 'services/resourcegroups'
import { Scope } from '@common/interfaces/SecretsInterface'
import type { UseStringsReturn } from 'framework/strings'
import RbacFactory from '@rbac/factories/RbacFactory'
import { getScopeFromDTO } from '@common/components/EntityReference/EntityReference'
 
export enum SelectorScope {
  CURRENT = 'CURRENT',
  INCLUDE_CHILD_SCOPES = 'INCLUDE_CHILD_SCOPES',
  CUSTOM = 'CUSTOM'
}
 
enum ValidatorTypes {
  BY_RESOURCE_IDENTIFIER = 'BY_RESOURCE_IDENTIFIER',
  BY_RESOURCE_TYPE = 'BY_RESOURCE_TYPE',
  BY_RESOURCE_TYPE_INCLUDING_CHILD_SCOPES = 'BY_RESOURCE_TYPE_INCLUDING_CHILD_SCOPES'
}
 
export const getSelectedResourcesMap = (
  resourceTypes: ResourceType[],
  resourceFilter?: ResourceFilter
): Map<ResourceType, string[] | string> => {
  const map: Map<ResourceType, string[] | string> = new Map()
  Iif (resourceFilter?.includeAllResources) {
    resourceTypes.forEach(resourceType => {
      map.set(resourceType, RbacResourceGroupTypes.DYNAMIC_RESOURCE_SELECTOR)
    })
  } else {
    resourceFilter?.resources?.map(resourceSelector => {
      Iif (resourceSelector.identifiers?.length) {
        map.set(resourceSelector.resourceType as ResourceType, resourceSelector.identifiers)
      } else {
        map.set(resourceSelector.resourceType as ResourceType, RbacResourceGroupTypes.DYNAMIC_RESOURCE_SELECTOR)
      }
    })
  }
 
  return map
}
 
export const validateResourceSelectors = (value: ResourceSelector[]): ResourceType[] => {
  return value
    .filter(val => {
      Eif (!isDynamicResourceSelector(val.type)) {
        if (val.identifiers?.length < 1) {
          return true
        }
      }
      return false
    })
    .map(val => val.resourceType)
}
 
export const getResourceSelectorsfromMap = (map: Map<ResourceType, string[] | string>): ResourceSelectorV2[] => {
  const resourceSelectors: ResourceSelectorV2[] = []
  map.forEach((value, key) => {
    if (isDynamicResourceSelector(value)) {
      resourceSelectors.push({
        resourceType: key
      })
    } else {
      resourceSelectors.push({
        resourceType: key,
        identifiers: value as string[]
      })
    }
  })
  return resourceSelectors
}
 
export const computeResourceMapOnChange = (
  setSelectedResourceMap: (value: React.SetStateAction<Map<ResourceType, string | string[]>>) => void,
  selectedResourcesMap: Map<ResourceType, string | string[]>,
  resourceType: ResourceType,
  isAdd: boolean,
  identifiers?: string[]
): void => {
  if (identifiers) {
    Eif (isAdd) {
      setSelectedResourceMap(
        produce(selectedResourcesMap, draft => {
          draft.set(resourceType, identifiers)
        })
      )
    } else {
      setSelectedResourceMap(
        produce(selectedResourcesMap, draft => {
          if (Array.isArray(draft.get(resourceType))) {
            draft.set(
              resourceType,
              (draft.get(resourceType) as string[]).filter(el => !identifiers.includes(el))
            )
          }
          if (draft.get(resourceType)?.length === 0) {
            draft.delete(resourceType)
          }
        })
      )
    }
  } else {
    Eif (isAdd) {
      setSelectedResourceMap(
        produce(selectedResourcesMap, draft => {
          draft.set(resourceType, RbacResourceGroupTypes.DYNAMIC_RESOURCE_SELECTOR)
        })
      )
    } else {
      setSelectedResourceMap(
        produce(selectedResourcesMap, draft => {
          draft.delete(resourceType)
        })
      )
    }
  }
}
 
export const computeResourceMapOnMultiChange = (
  setSelectedResourceMap: (value: React.SetStateAction<Map<ResourceType, string | string[]>>) => void,
  selectedResourcesMap: Map<ResourceType, string | string[]>,
  types: ResourceType[],
  isAdd: boolean
): void => {
  if (isAdd) {
    setSelectedResourceMap(
      produce(selectedResourcesMap, draft => {
        types.forEach(resourceType => {
          draft.set(resourceType, RbacResourceGroupTypes.DYNAMIC_RESOURCE_SELECTOR)
        })
      })
    )
  } else {
    setSelectedResourceMap(
      produce(selectedResourcesMap, draft => {
        types.forEach(resourceType => {
          draft.delete(resourceType)
        })
      })
    )
  }
}
 
export const getScopeLabelFromApi = (
  getString: UseStringsReturn['getString'],
  scope: Scope,
  resourceGroup: ResourceGroupV2
): string => {
  const selectorScope = getScopeType(resourceGroup)
  const dropDownItems = getScopeDropDownItems(scope, getString)
  const option = dropDownItems.filter(item => item.value === selectorScope)
  return option.length ? option[0].label : ''
}
 
export const getScopeDropDownItems = (scope: Scope, getString: UseStringsReturn['getString']): SelectOption[] => {
  switch (scope) {
    case Scope.PROJECT:
      return [{ label: getString('rbac.scopeItems.projectOnly'), value: SelectorScope.CURRENT }]
    case Scope.ORG:
      return [
        { label: getString('rbac.scopeItems.orgOnly'), value: SelectorScope.CURRENT },
        { label: getString('rbac.scopeItems.orgAll'), value: SelectorScope.INCLUDE_CHILD_SCOPES }
      ]
    case Scope.ACCOUNT:
    default:
      return [
        { label: getString('rbac.scopeItems.accountOnly'), value: SelectorScope.CURRENT },
        { label: getString('rbac.scopeItems.accountAll'), value: SelectorScope.INCLUDE_CHILD_SCOPES }
      ]
  }
}
 
export const getSelectionType = (resourceGroup?: ResourceGroupV2): SelectionType => {
  Iif (resourceGroup?.resourceFilter?.includeAllResources) {
    return SelectionType.ALL
  }
  return SelectionType.SPECIFIED
}
export const getScopeType = (resourceGroup?: ResourceGroupV2): SelectorScope => {
  Eif (resourceGroup?.includedScopes?.length) {
    for (const scope of resourceGroup?.includedScopes) {
      Eif (getScopeFromDTO(resourceGroup) === getScopeFromDTO(scope)) {
        return scope.filter === 'INCLUDING_CHILD_SCOPES' ? SelectorScope.INCLUDE_CHILD_SCOPES : SelectorScope.CURRENT
      } else {
        return SelectorScope.CUSTOM
      }
    }
  }
 
  return SelectorScope.CURRENT
}
 
export const getFormattedDataForApi = (
  data: ResourceGroupV2,
  selectionType: SelectionType,
  includedScopes: ScopeSelector[],
  resourceSelectors: ResourceSelectorV2[]
): ResourceGroupV2Request => {
  return {
    resourceGroup: {
      ...data,
      resourceFilter:
        selectionType === SelectionType.ALL
          ? {
              includeAllResources: true
            }
          : {
              resources: resourceSelectors,
              includeAllResources: false
            },
      includedScopes: includedScopes === [] ? getIncludedScopes(SelectorScope.CURRENT, data) : includedScopes
    }
  }
}
 
export const getFilteredResourceTypes = (
  data: ResponseResourceTypeDTO | null,
  selectedScope: SelectorScope
): ResourceType[] => {
  const resourceTypes = data?.data?.resourceTypes || []
  return resourceTypes?.reduce((acc: ResourceType[], value) => {
    if (
      value.name &&
      RbacFactory.isRegisteredResourceType(value.name as ResourceType) &&
      ((selectedScope === SelectorScope.CURRENT && value.validatorTypes?.includes(ValidatorTypes.BY_RESOURCE_TYPE)) ||
        selectedScope !== SelectorScope.CURRENT)
    ) {
      acc.push(value.name as ResourceType)
    }
    return acc
  }, [])
}
 
export const cleanUpResourcesMap = (
  resourceTypes: ResourceType[],
  selectedResourcesMap: Map<ResourceType, string | string[]>,
  selectionType: SelectionType
): Map<ResourceType, string | string[]> => {
  const types = [...resourceTypes]
  selectedResourcesMap.forEach((_value, key) => {
    Iif (!types.includes(key)) {
      selectedResourcesMap.delete(key)
    } else {
      types.splice(types.indexOf(key), 1)
      selectedResourcesMap.set(key, RbacResourceGroupTypes.DYNAMIC_RESOURCE_SELECTOR)
    }
  })
  Iif (selectionType === SelectionType.ALL) {
    types.map(resourceType => {
      selectedResourcesMap.set(resourceType, RbacResourceGroupTypes.DYNAMIC_RESOURCE_SELECTOR)
    })
  }
  return selectedResourcesMap
}
 
export const getIncludedScopes = (type: SelectorScope, resourceGroup: ResourceGroupV2): ScopeSelector[] => {
  const { accountIdentifier, orgIdentifier, projectIdentifier } = resourceGroup
  switch (type) {
    case SelectorScope.CUSTOM:
      //Replace with custom scope once it is added
      return []
    case SelectorScope.INCLUDE_CHILD_SCOPES:
      return [
        {
          accountIdentifier,
          orgIdentifier,
          projectIdentifier,
          filter: 'INCLUDING_CHILD_SCOPES'
        }
      ]
    default:
      return [
        {
          accountIdentifier,
          orgIdentifier,
          projectIdentifier,
          filter: 'EXCLUDING_CHILD_SCOPES'
        }
      ]
  }
}