|
| 1 | +// Copyright (c) 2021 Uber Technologies Inc. |
| 2 | +// |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +import { |
| 23 | + ROUTE_PARAMS_CLUSTER_NAME, |
| 24 | + ROUTE_PARAMS_DOMAIN, |
| 25 | +} from '../route/getter-types'; |
| 26 | +import { |
| 27 | + CROSS_REGION_ALLOWED_CROSS_ORIGIN, |
| 28 | + CROSS_REGION_CLUSTER_ORIGIN_LIST, |
| 29 | +} from '../cross-region/getter-types'; |
| 30 | +import { |
| 31 | + DOMAIN_CHANGE_ORIGIN, |
| 32 | + DOMAIN_FETCH, |
| 33 | + DOMAIN_ON_MOUNT, |
| 34 | +} from './action-types'; |
| 35 | +import { DOMAIN_CROSS_ORIGIN, DOMAIN_IS_READY } from './getter-types'; |
| 36 | +import { |
| 37 | + DOMAIN_RESET_STATE, |
| 38 | + DOMAIN_SET_DOMAIN, |
| 39 | + DOMAIN_SET_ERROR, |
| 40 | +} from './mutation-types'; |
| 41 | +import { httpService } from '~services'; |
| 42 | +import { getExpiryDateTimeFromNow } from '~helpers'; |
| 43 | + |
| 44 | +const actions = { |
| 45 | + [DOMAIN_FETCH]: async ({ commit, getters }) => { |
| 46 | + const clusterName = getters[ROUTE_PARAMS_CLUSTER_NAME]; |
| 47 | + const domainName = getters[ROUTE_PARAMS_DOMAIN]; |
| 48 | + const ready = getters[DOMAIN_IS_READY]; |
| 49 | + const allowedCrossOrigin = getters[CROSS_REGION_ALLOWED_CROSS_ORIGIN]; |
| 50 | + const clusterOriginList = getters[CROSS_REGION_CLUSTER_ORIGIN_LIST]; |
| 51 | + |
| 52 | + if (ready) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + commit(DOMAIN_RESET_STATE, domainName); |
| 57 | + |
| 58 | + const cluster = |
| 59 | + allowedCrossOrigin && |
| 60 | + clusterName && |
| 61 | + clusterOriginList && |
| 62 | + clusterOriginList.find( |
| 63 | + ({ clusterName: matchClusterName }) => matchClusterName === clusterName |
| 64 | + ); |
| 65 | + const origin = (cluster && cluster.origin) || ''; |
| 66 | + |
| 67 | + try { |
| 68 | + const domain = await httpService.get( |
| 69 | + `${origin}/api/domains/${domainName}` |
| 70 | + ); |
| 71 | + |
| 72 | + if (allowedCrossOrigin && !domain.isGlobalDomain) { |
| 73 | + const fetchList = clusterOriginList |
| 74 | + .filter( |
| 75 | + ({ clusterName }) => |
| 76 | + clusterName !== domain.replicationConfiguration.activeClusterName |
| 77 | + ) |
| 78 | + .map(({ origin }) => async () => { |
| 79 | + try { |
| 80 | + const domainConfig = await httpService.get( |
| 81 | + `${origin}/api/domains/${domainName}` |
| 82 | + ); |
| 83 | + |
| 84 | + return domainConfig; |
| 85 | + } catch (error) { |
| 86 | + console.warn( |
| 87 | + `Unable to resolve domain configuration for domain = "${domainName}" and origin = "${origin}".` |
| 88 | + ); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + const domainList = ( |
| 93 | + await Promise.all(fetchList.map(callback => callback())) |
| 94 | + ).filter(response => !!response); |
| 95 | + |
| 96 | + domainList.forEach(localDomain => { |
| 97 | + localDomain.expiryDateTime = getExpiryDateTimeFromNow(); |
| 98 | + commit(DOMAIN_SET_DOMAIN, localDomain); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + domain.expiryDateTime = getExpiryDateTimeFromNow(); |
| 103 | + commit(DOMAIN_SET_DOMAIN, domain); |
| 104 | + } catch (error) { |
| 105 | + console.warn( |
| 106 | + `Failed to fetch domain configuration for "${domainName}" from "${origin}".` |
| 107 | + ); |
| 108 | + console.warn(error); |
| 109 | + commit(DOMAIN_SET_ERROR, { |
| 110 | + domainName, |
| 111 | + error: `An error occurred while trying to fetch domain "${domainName}". Please check the domain is correct and try again.`, |
| 112 | + }); |
| 113 | + } |
| 114 | + }, |
| 115 | + [DOMAIN_ON_MOUNT]: ({ dispatch }) => { |
| 116 | + dispatch(DOMAIN_CHANGE_ORIGIN); |
| 117 | + dispatch(DOMAIN_FETCH); |
| 118 | + }, |
| 119 | + [DOMAIN_CHANGE_ORIGIN]: ({ getters }) => { |
| 120 | + const origin = getters[DOMAIN_CROSS_ORIGIN]; |
| 121 | + |
| 122 | + httpService.setOrigin(origin); |
| 123 | + }, |
| 124 | +}; |
| 125 | + |
| 126 | +export default actions; |
0 commit comments