|
| 1 | +import { google } from '@google-cloud/compute/build/protos/protos' |
| 2 | + |
| 3 | +import cuid from 'cuid' |
| 4 | +import { RawGcpCdnBackendService } from './data' |
| 5 | +import { enumKeyToString } from '../../utils/format' |
| 6 | +import { |
| 7 | + GcpCdnBackendService, |
| 8 | + GcpCdnBackendServiceBackend, |
| 9 | + GcpCdnBackendServiceCdnPolicy, |
| 10 | + GcpCdnBackendServiceCdnPolicyNegativeCachingPolicy, |
| 11 | + GcpCdnBackendServiceOutlierDetection, |
| 12 | +} from '../../types/generated' |
| 13 | +import { toISOString } from '../../utils/dateutils' |
| 14 | + |
| 15 | +const formatBackend = ({ |
| 16 | + balancingMode, |
| 17 | + capacityScaler, |
| 18 | + description, |
| 19 | + failover, |
| 20 | + group, |
| 21 | + maxConnections, |
| 22 | + maxConnectionsPerEndpoint, |
| 23 | + maxConnectionsPerInstance, |
| 24 | + maxRate, |
| 25 | + maxRatePerEndpoint, |
| 26 | + maxRatePerInstance, |
| 27 | + maxUtilization, |
| 28 | +}: google.cloud.compute.v1.IBackend): GcpCdnBackendServiceBackend => { |
| 29 | + return { |
| 30 | + id: cuid(), |
| 31 | + balancingMode: enumKeyToString(google.cloud.compute.v1.Backend.BalancingMode, balancingMode), |
| 32 | + capacityScaler, |
| 33 | + description, |
| 34 | + failover, |
| 35 | + group, |
| 36 | + maxConnections, |
| 37 | + maxConnectionsPerEndpoint, |
| 38 | + maxConnectionsPerInstance, |
| 39 | + maxRate, |
| 40 | + maxRatePerEndpoint, |
| 41 | + maxRatePerInstance, |
| 42 | + maxUtilization, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const formatCdnPolicyNegativeCachingPolicy = ({ |
| 47 | + code, |
| 48 | + ttl, |
| 49 | +}: google.cloud.compute.v1.IBackendServiceCdnPolicyNegativeCachingPolicy): GcpCdnBackendServiceCdnPolicyNegativeCachingPolicy => { |
| 50 | + return { |
| 51 | + id: cuid(), |
| 52 | + code, |
| 53 | + ttl, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +const formatCdnPolicy = ({ |
| 58 | + bypassCacheOnRequestHeaders = [], |
| 59 | + cacheKeyPolicy, |
| 60 | + cacheMode, |
| 61 | + clientTtl, |
| 62 | + defaultTtl, |
| 63 | + maxTtl, |
| 64 | + negativeCaching, |
| 65 | + negativeCachingPolicy = [], |
| 66 | + requestCoalescing, |
| 67 | + serveWhileStale, |
| 68 | + signedUrlCacheMaxAgeSec, |
| 69 | + signedUrlKeyNames, |
| 70 | +}: google.cloud.compute.v1.IBackendServiceCdnPolicy): GcpCdnBackendServiceCdnPolicy => { |
| 71 | + return { |
| 72 | + bypassCacheOnRequestHeaderNames: bypassCacheOnRequestHeaders?.map( |
| 73 | + header => header?.headerName || '' |
| 74 | + ), |
| 75 | + cacheKeyPolicyIncludeHost: cacheKeyPolicy?.includeHost || false, |
| 76 | + cacheKeyPolicyIncludeProtocol: cacheKeyPolicy?.includeProtocol || false, |
| 77 | + cacheKeyPolicyIncludeQueryString: cacheKeyPolicy?.includeQueryString || false, |
| 78 | + cacheKeyPolicyQueryStringBlacklist: cacheKeyPolicy?.queryStringBlacklist || [], |
| 79 | + cacheKeyPolicyQueryStringWhitelist: cacheKeyPolicy?.queryStringWhitelist || [], |
| 80 | + cacheMode: enumKeyToString(google.cloud.compute.v1.BackendServiceCdnPolicy.CacheMode, cacheMode), |
| 81 | + clientTtl, |
| 82 | + defaultTtl, |
| 83 | + maxTtl, |
| 84 | + negativeCaching, |
| 85 | + negativeCachingPolicy: negativeCachingPolicy?.map(policy => formatCdnPolicyNegativeCachingPolicy(policy)), |
| 86 | + requestCoalescing, |
| 87 | + serveWhileStale, |
| 88 | + signedUrlCacheMaxAgeSec: signedUrlCacheMaxAgeSec?.toString() || '', |
| 89 | + signedUrlKeyNames, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +const formatOutlierDetection = ({ |
| 94 | + baseEjectionTime, |
| 95 | + consecutiveErrors, |
| 96 | + consecutiveGatewayFailure, |
| 97 | + enforcingConsecutiveErrors, |
| 98 | + enforcingConsecutiveGatewayFailure, |
| 99 | + enforcingSuccessRate, |
| 100 | + interval, |
| 101 | + maxEjectionPercent, |
| 102 | + successRateMinimumHosts, |
| 103 | + successRateRequestVolume, |
| 104 | + successRateStdevFactor, |
| 105 | +}: google.cloud.compute.v1.IOutlierDetection): GcpCdnBackendServiceOutlierDetection => { |
| 106 | + return { |
| 107 | + baseEjectionTime: toISOString(baseEjectionTime?.seconds?.toString()) || '', |
| 108 | + consecutiveErrors, |
| 109 | + consecutiveGatewayFailure, |
| 110 | + enforcingConsecutiveErrors, |
| 111 | + enforcingConsecutiveGatewayFailure, |
| 112 | + enforcingSuccessRate, |
| 113 | + interval: toISOString(interval?.seconds?.toString()) || '', |
| 114 | + maxEjectionPercent, |
| 115 | + successRateMinimumHosts, |
| 116 | + successRateRequestVolume, |
| 117 | + successRateStdevFactor, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +export default ({ |
| 122 | + service, |
| 123 | + account, |
| 124 | + region, |
| 125 | +}: { |
| 126 | + service: RawGcpCdnBackendService |
| 127 | + account: string |
| 128 | + region: string |
| 129 | +}): GcpCdnBackendService => { |
| 130 | + const { |
| 131 | + affinityCookieTtlSec, |
| 132 | + backends = [], |
| 133 | + cdnPolicy = {}, |
| 134 | + circuitBreakers, |
| 135 | + connectionDraining, |
| 136 | + consistentHash, |
| 137 | + creationTimestamp, |
| 138 | + customRequestHeaders, |
| 139 | + customResponseHeaders, |
| 140 | + description, |
| 141 | + enableCDN, |
| 142 | + failoverPolicy, |
| 143 | + fingerprint, |
| 144 | + healthChecks, |
| 145 | + iap, |
| 146 | + id, |
| 147 | + kind, |
| 148 | + loadBalancingScheme, |
| 149 | + localityLbPolicy, |
| 150 | + logConfig, |
| 151 | + maxStreamDuration, |
| 152 | + name, |
| 153 | + outlierDetection = {}, |
| 154 | + port, |
| 155 | + portName, |
| 156 | + protocol, |
| 157 | + securityPolicy, |
| 158 | + securitySettings, |
| 159 | + selfLink, |
| 160 | + sessionAffinity, |
| 161 | + timeoutSec, |
| 162 | + } = service |
| 163 | + |
| 164 | + return { |
| 165 | + id, |
| 166 | + projectId: account, |
| 167 | + region, |
| 168 | + name, |
| 169 | + affinityCookieTtlSec, |
| 170 | + backends: backends?.map(backend => formatBackend(backend)), |
| 171 | + cdnPolicy: formatCdnPolicy(cdnPolicy), |
| 172 | + circuitBreakersMaxConnections: circuitBreakers?.maxConnections || 0, |
| 173 | + circuitBreakersMaxPendingRequests: circuitBreakers?.maxPendingRequests || 0, |
| 174 | + circuitBreakersMaxRequests: circuitBreakers?.maxRequests || 0, |
| 175 | + circuitBreakersMaxRequestsPerConnection: circuitBreakers?.maxRequestsPerConnection || 0, |
| 176 | + circuitBreakersMaxRetries: circuitBreakers?.maxRetries || 0, |
| 177 | + connectionDrainingTimeoutSec: connectionDraining?.drainingTimeoutSec || 0, |
| 178 | + consistentHashLoadBalancerSettingHttpCookieName: consistentHash?.httpCookie?.name || '', |
| 179 | + consistentHashLoadBalancerSettingHttpCookiePath: consistentHash?.httpCookie?.path || '', |
| 180 | + consistentHashLoadBalancerSettingHttpCookie: toISOString(consistentHash?.httpCookie?.ttl.seconds?.toString()) || '', |
| 181 | + consistentHashLoadBalancerSettingHttpHeaderName: consistentHash?.httpHeaderName || '', |
| 182 | + consistentHashLoadBalancerSettingMinimumRingSize: consistentHash?.minimumRingSize?.toString() || '', |
| 183 | + creationTimestamp, |
| 184 | + customRequestHeaders, |
| 185 | + customResponseHeaders, |
| 186 | + description, |
| 187 | + enableCDN, |
| 188 | + failoverPolicyDisableConnectionDrainOnFailover: failoverPolicy?.disableConnectionDrainOnFailover || false, |
| 189 | + failoverPolicyDropTrafficIfUnhealthy: failoverPolicy?.dropTrafficIfUnhealthy || false, |
| 190 | + failoverPolicyFailoverRatio: failoverPolicy?.failoverRatio || 0, |
| 191 | + fingerprint, |
| 192 | + healthChecks, |
| 193 | + iapEnabled: iap?.enabled || false, |
| 194 | + iapOauth2ClientId: iap?.oauth2ClientId || '', |
| 195 | + // remove the secrets |
| 196 | + // oauth2ClientSecret |
| 197 | + // oauth2ClientSecretSha256 |
| 198 | + kind, |
| 199 | + loadBalancingScheme: enumKeyToString(google.cloud.compute.v1.BackendService.LoadBalancingScheme, loadBalancingScheme), |
| 200 | + localityLbPolicy: enumKeyToString(google.cloud.compute.v1.BackendService.LocalityLbPolicy, localityLbPolicy), |
| 201 | + logConfigEnable: logConfig?.enable || false, |
| 202 | + logConfigSampleRate: logConfig?.sampleRate || 0, |
| 203 | + maxStreamDuration: toISOString(maxStreamDuration?.seconds?.toString()), |
| 204 | + outlierDetection: formatOutlierDetection(outlierDetection), |
| 205 | + port, |
| 206 | + portName, |
| 207 | + protocol: enumKeyToString(google.cloud.compute.v1.BackendService.Protocol, protocol), |
| 208 | + securityPolicy, |
| 209 | + securitySettingClientTlsPolicy: securitySettings?.clientTlsPolicy || '', |
| 210 | + securitySettingSubjectAltNames: securitySettings?.subjectAltNames || [], |
| 211 | + selfLink, |
| 212 | + sessionAffinity: enumKeyToString(google.cloud.compute.v1.BackendService.SessionAffinity, sessionAffinity), |
| 213 | + timeoutSec, |
| 214 | + } |
| 215 | +} |
0 commit comments