#   Copyright 2022 Google LLC
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
cloudresourcemanager.googleapis.com/Organization:
  label: |
    {{ .Resource.Data.displayName }}

cloudresourcemanager.googleapis.com/Folder:
  link: |
    https://console.cloud.google.com/cloud-resource-manager?folder={{ GetLastPart .Resource.Data.name }}
  label: |
    {{ .Resource.Data.displayName }}

cloudresourcemanager.googleapis.com/Project:
  link: |
    https://console.cloud.google.com/home/dashboard?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    {{ .Resource.Data.name }}

# Suppress Compute Engine projects
compute.googleapis.com/Project:
  link: ""
  label: ""

compute.googleapis.com/Network:
  link: |
    https://console.cloud.google.com/networking/networks/details/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    Network|{{ "{" }}{{ .Resource.Data.name }}|{{ if .Resource.Data.subnetworkConfig }}{{ with $snc := .Resource.Data.subnetworkConfig }}{{ range $i, $s := $snc }}{{ $s.ipCidrRange }}{{if NotLast $i $snc }}|{{end}}{{end}}{{end}}{{else}}{{ if .Resource.Data.subnetworks }}({{len .Resource.Data.subnetworks }} subnets){{end}}{{end}} }

compute.googleapis.com/Subnetwork:
  label: |
    <<u>{{ .Resource.Data.name }}</u>
    {{ GetRegion .Resource.Data.region }}: {{ .Resource.Data.ipCidrRange }}
    {{if .Resource.Data.secondaryIpRanges }}
    {{range .Resource.Data.secondaryIpRanges}}{{ .rangeName }}: {{ .ipCidrRange }}
    {{end}}{{end}}>

compute.googleapis.com/Instance:
  link: |
    https://console.cloud.google.com/compute/instancesDetail/zones/{{ GetLastPart .Resource.Data.zone | urlquery }}/instances/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    {{ .Resource.Data.name }} ({{with $nif := index .Resource.Data.networkInterfaces 0}}{{ $nif.networkIP }}{{ end }})
    {{ GetLastPart .Resource.Data.machineType }}

compute.googleapis.com/Router:
  link: |
    https://console.cloud.google.com/hybrid/routers/details/{{ GetLastPart .Resource.Data.region | urlquery }}/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    {{ if .Resource.Data.nats }}Cloud NAT:
    {{else}}Cloud Router:
    {{end}}{{ .Resource.Data.name }}
    {{ if .Resource.Data.bgp }}BGP: {{ .Resource.Data.bgp.advertiseMode }}{{end}}
  tailLabel: |
    {{ if.Resource.Data.bgp }}ASN {{ .Resource.Data.bgp.asn }}{{end}}

compute.googleapis.com/InterconnectAttachment:
  link: |
    https://console.cloud.google.com/hybrid/attachments/details/{{ GetLastPart .Resource.Data.region | urlquery }}/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    Interconnect attachment:
    {{ .Resource.Data.name }} ({{ .Resource.Data.type }} {{ .Resource.Data.bandwidth }})
    VLAN: {{ .Resource.Data.vlanTag8021q }}

compute.googleapis.com/Interconnect:
  link: |
    https://console.cloud.google.com/hybrid/interconnects/details/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    Interconnect:
    {{ .Resource.Data.name }} ({{ .Resource.Data.linkType }})
    {{ .Resource.Data.circuitInfo.googleCircuitId }} / {{ .Resource.Data.circuitInfo.googleDemarcId }}
       
compute.googleapis.com/VpnTunnel:
  link: |
    https://console.cloud.google.com/hybrid/vpn/tunnels/details/{{ GetLastPart .Resource.Data.region | urlquery }}/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  tailLabel: |
    {{ .Resource.Data.peerIp }}
  label: |
    VPN tunnel:
    {{ .Resource.Data.name }}
    {{ .Resource.Data.status }}

compute.googleapis.com/VpnGateway:
  link: |
    https://console.cloud.google.com/hybrid/vpn/gateways/details/{{ GetLastPart .Resource.Data.region | urlquery }}/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  tailLabel: |
    {{ with $vif := .Resource.Data.vpnInterface }}{{ range $i, $v := $vif }}{{ .ipAddress }}{{if NotLast $i $vif }}, {{end }}{{end}}{{end}}
  label: |
    VPN gateway: 
    {{ .Resource.Data.name }}
    {{ with $vif := .Resource.Data.vpnInterface }}{{ range $i, $v := $vif }}{{ .ipAddress }}{{if NotLast $i $vif }}, {{end }}{{end}}{{end}}
    

compute.googleapis.com/Address:
  label: |
    {{ .Resource.Data.name }} 
    {{ .Resource.Data.address }}
    {{ .Resource.Data.addressType }}{{ if .Resource.Data.purpose }} | {{ .Resource.Data.purpose }}{{end}}

compute.googleapis.com/GlobalAddress:
  label: |
    {{ .Resource.Data.name }} 
    {{ .Resource.Data.address }}
    {{ .Resource.Data.addressType }} | GLOBAL

compute.googleapis.com/Autoscaler:
  label: |
    Autoscaler: {{ .Resource.Data.name }} 
    {{ .Resource.Data.autoscalingPolicy.minNumReplicas }}-{{ .Resource.Data.autoscalingPolicy.maxNumReplicas }} replicas

compute.googleapis.com/BackendBucket:
  label: |
    {{ .Resource.Data.name }} 
    ({{ .Resource.Data.bucketName }})

compute.googleapis.com/BackendService:
  label: |
    Backend:
    {{ .Resource.Data.name }} 
    ({{ .Resource.Data.protocol }}{{ if .Resource.Data.port }} port {{ .Resource.Data.port }}{{end}})

compute.googleapis.com/RegionBackendService:
  label: |
    Region backend: {{ .Resource.Data.name }} 
    ({{ .Resource.Data.protocol }}{{ if .Resource.Data.port }} port {{ .Resource.Data.port }}{{end}})

compute.googleapis.com/NetworkEndpointGroup:
  label: |
    NEG: {{ .Resource.Data.name }} 
    {{ GetLastPart .Resource.Data.zone }} | {{ .Resource.Data.size }} of {{ .Resource.Data.networkEndpointType }}
    
compute.googleapis.com/Disk:
  label: |
    {{ GetLastPart .Resource.Data.type }}: {{ .Resource.Data.name }} 
    ({{ .Resource.Data.sizeGb }} GB)

compute.googleapis.com/Firewall:
  label: |
    {{ .Resource.Data.name }}
    {{ .Resource.Data.direction }} {{ if .Resource.Data.allowed }} ALLOW: 
      {{ range .Resource.Data.allowed }}{{ .IPProtocol }}{{ if .ports }}: {{ JoinNicely .ports }}{{end}} | {{end}}{{end}}{{ if .Resource.Data.denied }} DENY: 
      {{ range .Resource.Data.denied }}{{ .IPProtocol }}{{ if .ports }}: {{ JoinNicely .ports }}{{end}} | {{end}}{{end}}
    {{ if eq .Resource.Data.direction "INGRESS" }}{{ if .Resource.Data.sourceRanges }}{{ JoinNicely .Resource.Data.sourceRanges }}{{ else }}{{ if .Resource.Data.sourceServiceAccounts }}{{ JoinNicely .Resource.Data.sourceServiceAccounts }}{{ else }}{{ if .Resource.Data.sourceTags }}{{ JoinNicely .Resource.Data.sourceTags }}{{ end }}{{ end }}{{ end }} ⟶ {{ if .Resource.Data.targetTags }}{{ JoinNicely .Resource.Data.targetTags }}{{ else }}{{ if .Resource.Data.targetServiceAccounts }}{{ JoinNicely .Resource.Data.targetServiceAccounts }}{{ end }}{{ end }} {{end}}
    {{ if eq .Resource.Data.direction "EGRESS" }}{{ if .Resource.Data.sourceTags }}{{ JoinNicely .Resource.Data.sourceTags }}{{ else }}{{ if .Resource.Data.sourceServiceAccounts }}{{ JoinNicely .Resource.Data.sourceServiceAccounts }}{{ else }}ALL{{end}}{{end}} ⟶ {{ if .Resource.Data.targetServiceAccounts }}{{ JoinNicely .Resource.Data.targetServiceAccounts }}{{ else }}{{ if .Resource.Data.targetTags }}{{ JoinNicely .Resource.Data.targetTags }}{{end}}{{end}} {{ if .Resource.Data.destinationRanges }}({{ JoinNicely .Resource.Data.destinationRanges }}){{end}} {{end}}

compute.googleapis.com/HealthCheck:
  label: |
    {{ .Resource.Data.type }} healthcheck: 
    {{ .Resource.Data.name }} 

compute.googleapis.com/HttpHealthCheck:
  label: |
    HTTP healthcheck: 
    {{ .Resource.Data.name }} 
    
compute.googleapis.com/Image:
  label: |
    Image: {{ .Resource.Data.name }} 
    ({{ .Resource.Data.diskSizeGb }} GB)

compute.googleapis.com/InstanceTemplate:
  label: |
    Template: {{ .Resource.Data.name }}
    {{ GetLastPart .Resource.Data.properties.machineType }}

compute.googleapis.com/InstanceGroup:
  label: |
    {{ .Resource.Data.name }} 
    (size {{ .Resource.Data.size }})

compute.googleapis.com/License:
  label: |
    Image: {{ .Resource.Data.name }} 
    ({{ .Resource.Data.licenseCode }})
    
compute.googleapis.com/Route:
  label: |
    {{ .Resource.Data.name }}
    {{ .Resource.Data.destRange }}

compute.googleapis.com/Snapshot:
  label: |
    Snapshot: {{ .Resource.Data.name }} 

compute.googleapis.com/SslCertificate:
  label: |
    TLS certificate: {{ .Resource.Data.name }}
    {{ if .Resource.Data.managed }}{{ JoinNicely .Resource.Data.managed.domains }}: {{ .Resource.Data.managed.status }}{{end}}
    {{ DaysLeft .Resource.Data.expireTime }} days left

compute.googleapis.com/TargetHttpProxy:
  label: |
    Target proxy: {{ .Resource.Data.name }}
    HTTP
    
compute.googleapis.com/TargetHttpsProxy:
  label: |
    Target proxy: {{ .Resource.Data.name }}
    HTTPS

compute.googleapis.com/TargetTcpProxy:
  label: |
    Target proxy: {{ .Resource.Data.name }}
    TCP
    
compute.googleapis.com/TargetSslProxy:
  label: |
    Target proxy: {{ .Resource.Data.name }}
    SSL

compute.googleapis.com/TargetPool:
  label: |
    Target pool: {{ .Resource.Data.name }}

compute.googleapis.com/TargetVpnGateway:
  label: |
    Target VPN gateway:
    {{ .Resource.Data.name }}
    (status {{ .Resource.Data.status }})    
    
compute.googleapis.com/UrlMap:
  label: |
    URL map:
    {{ .Resource.Data.name }}
        
appengine.googleapis.com/Application:
  label: |
    {{ .Resource.Data.name }}
    
appengine.googleapis.com/Service:
  label: |
    {{ .Resource.Data.name }}
    
appengine.googleapis.com/Version:
  label: |
    {{ .Resource.Data.name }}
    
cloudbilling.googleapis.com/BillingAccount:
  label: |
    {{ .Resource.Data.displayName }}

storage.googleapis.com/Bucket:
  label: |
    gs://{{ .Resource.Data.name }}/
    {{if len .Resource.Data.location | lt 4}}{{ ToLower .Resource.Data.location }}{{else}}{{ .Resource.Data.location }}{{end}} {{ .Resource.Data.locationType }} | {{ .Resource.Data.storageClass }}

dns.googleapis.com/ManagedZone:
  label: |
    {{ .Resource.Data.name }}
    {{ .Resource.Data.dnsName }}
    ({{ .Resource.Data.visibility }})
    
dns.googleapis.com/Policy:
  label: |
    {{ .Resource.Data.name }}

spanner.googleapis.com/Instance:
  label: |
    {{ .Resource.Data.name }}
    ({{ .Resource.Data.nodeCount }} nodes)
        
spanner.googleapis.com/Database:
  label: |
    {{ GetLastPart .Resource.Data.name }}

bigquery.googleapis.com/Dataset:
  label: |
    {{ .Resource.Data.id }}
    {{ .Resource.Data.location }}

iam.googleapis.com/ServiceAccount:
  label: |
    {{ .Resource.Data.displayName }}
    {{ .Resource.Data.email }}

iam.googleapis.com/ServiceAccountKey:
  label: |
    {{ GetLastPart .Resource.Data.name }}
    {{ .Resource.Data.keyOrigin }} | {{ .Resource.Data.keyType }}
    
cloudkms.googleapis.com/KeyRing:
  label: |
    {{ GetLastPart .Resource.Data.name }}

cloudkms.googleapis.com/CryptoKeyVersion:
  label: |
    {{ GetLastPart .Resource.Data.name }}
    Protection: {{ .Resource.Data.protectionLevel }}
    
container.googleapis.com/Cluster:
  link: |
    https://console.cloud.google.com/kubernetes/clusters/details/{{ urlquery .Resource.Data.location }}/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    GKE cluster: {{ .Resource.Data.name }}
    {{ .Resource.Data.location }}, {{ .Resource.Data.currentNodeCount }} nodes
    Master: {{ .Resource.Data.currentMasterVersion }}, nodes: {{ .Resource.Data.currentNodeVersion }}

container.googleapis.com/NodePool:
  link: |
    https://console.cloud.google.com/kubernetes/nodepool/{{ GetPartFromEnd .Resource.Data.selfLink 5 | urlquery }}/{{ GetPartFromEnd .Resource.Data.selfLink 3 | urlquery }}/{{ urlquery .Resource.Data.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    Node pool: {{ .Resource.Data.name }}
    {{ .Resource.Data.config.machineType }}, {{ .Resource.Data.initialNodeCount }} nodes
    {{ Join .Resource.Data.locations }}

sqladmin.googleapis.com/Instance:
  label: |
    {{ .Resource.Data.name }} ({{ .Resource.Data.databaseVersion }})
    {{ .Resource.Data.settings.tier }}, {{ .Resource.Data.settings.dataDiskSizeGb }} GB      

logging.googleapis.com/LogSink:
  label: |
    Log sink: {{ .Resource.Data.name }}
    To: {{ Replace "bigquery.googleapis.com/" "" .Resource.Data.destination | Replace "storage.googleapis.com/" "" }}

logging.googleapis.com/LogMetric:
  label: |
    {{ .Resource.Data.name }}
    {{ .Resource.Data.filter }}

compute.googleapis.com/ForwardingRule:
  label: |
    Forwarding rule:
    {{ .Resource.Data.name }} 
    {{ .Resource.Data.IPProtocol }} {{ .Resource.Data.IPAddress }}:{{ if .Resource.Data.ports }}{{ JoinNicely .Resource.Data.ports }}{{else}}{{ .Resource.Data.portRange }}{{end}}

compute.googleapis.com/GlobalForwardingRule:
  label: |
    Global forwarding rule:
    {{ .Resource.Data.name }} 
    {{ .Resource.Data.IPProtocol }} {{ .Resource.Data.IPAddress }}:{{ if .Resource.Data.ports }}{{ JoinNicely .Resource.Data.ports }}{{else}}{{ .Resource.Data.portRange }}{{end}}

compute.googleapis.com/InstanceGroupManager:
  label: |
    {{ .Resource.Data.name }} 
    (target size {{ .Resource.Data.targetSize }})
    
compute.googleapis.com/RegionDisk:
  label: |
    Regional {{ GetLastPart .Resource.Data.type }}: {{ .Resource.Data.name }}
    ({{ .Resource.Data.sizeGb }} GB)
                  
bigquery.googleapis.com/Table:
  label: |
    {{ .Resource.Data.tableReference.tableId }}

pubsub.googleapis.com/Topic:
  label: |
    Topic: {{ .Resource.Data.name }}

pubsub.googleapis.com/Subscription:
  label: |
    Subscription: {{ GetLastPart .Resource.Data.name }}

compute.googleapis.com/SecurityPolicy:
  label: |
    Cloud Armor:
    {{ .Resource.Data.name }}

k8s.io/Node:
  link: |
    https://console.cloud.google.com/kubernetes/node/{{ GetPartFromEnd .Resource.Parent 3 | urlquery}}/{{ GetLastPart .Resource.Parent | urlquery}}/{{ urlquery .Resource.Data.metadata.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    {{ .Resource.Data.metadata.name }}
    Kubelet: {{ .Resource.Data.status.nodeInfo.kubeletVersion }}
    CPU free: {{ .Resource.Data.status.allocatable.cpu }}
    Memory free: {{ .Resource.Data.status.allocatable.memory }}

k8s.io/Namespace:
  link: |
    https://console.cloud.google.com/kubernetes/workload?pageState=("savedViews":("i":"1","c":%5B"gke%2F{{ GetPartFromEnd .Resource.Parent 3 | urlquery}}%2F{{ GetLastPart .Resource.Parent | urlquery}}"%5D,"n":%5B"{{ urlquery .Resource.Data.metadata.name }}"%5D))&project={{ index .Ancestors 0 | GetLastPart }}&p
  label: |
    Namespace: {{ .Resource.Data.metadata.name }}

k8s.io/Pod:
  link: |
    https://console.cloud.google.com/kubernetes/pod/{{ GetPartFromEnd .Resource.Parent 6 | urlquery}}/{{ GetPartFromEnd .Resource.Parent 4 | urlquery}}/{{ GetLastPart .Resource.Parent | urlquery}}/{{ urlquery .Resource.Data.metadata.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    Pod: {{ .Resource.Data.metadata.name }}
    {{ .Resource.Data.status.podIP }} | {{ .Resource.Data.status.phase }}

k8s.io/Service:
  link: |
    https://console.cloud.google.com/kubernetes/service/{{ GetPartFromEnd .Resource.Parent 6 | urlquery}}/{{ GetPartFromEnd .Resource.Parent 4 | urlquery}}/{{ GetLastPart .Resource.Parent | urlquery}}/{{ urlquery .Resource.Data.metadata.name }}?project={{ index .Ancestors 0 | GetLastPart }}
  label: |
    Service: {{ .Resource.Data.metadata.name }}
    {{ .Resource.Data.spec.clusterIP }} | {{ .Resource.Data.spec.type }}

networking.k8s.io/Ingress:
  label: |
    Ingress: {{ .Resource.Data.metadata.name }}
    {{ if .Resource.Data.status.loadBalancer.ingress }}{{ range .Resource.Data.status.loadBalancer.ingress }}{{ .ip }} | {{end}}{{end}}

extensions.k8s.io/Ingress:
  label: |
    Ingress: {{ .Resource.Data.metadata.name }}
    {{ if .Resource.Data.status.loadBalancer.ingress }}{{ range .Resource.Data.status.loadBalancer.ingress }}{{ .ip }} | {{end}}{{end}}
    
rbac.authorization.k8s.io/RoleBinding:
  label: |
    RoleBinding: {{ .Resource.Data.metadata.name }}
    {{ range .Resource.Data.subjects }}{{ .kind }}:{{ .name }} {{end}} ⟶ {{ .Resource.Data.roleRef.kind }}:{{ .Resource.Data.roleRef.name }}

rbac.authorization.k8s.io/ClusterRoleBinding:
  label: |
    RoleBinding: {{ .Resource.Data.metadata.name }}
    {{ range .Resource.Data.subjects }}{{ .kind }}:{{ .name }} {{end}} ⟶ {{ .Resource.Data.roleRef.kind }}:{{ .Resource.Data.roleRef.name }}

rbac.authorization.k8s.io/Role:
  label: |
    {{ "{" }}Role: {{ .Resource.Data.metadata.name }}{{ with $rules := .Resource.Data.rules }}{{ "|{" }}Groups|Names|Resources|Verbs{{ "}" }}{{ range $i, $r := $rules }}{{ "|{" }} {{ JoinNicely .apiGroups }}|{{ JoinNicely .resourceNames }}|{{ JoinNicely .resources }}|{{ JoinNicely .verbs }}{{ "}" }} {{end}}{{end}}{{ "}" }}

rbac.authorization.k8s.io/ClusterRole:
  label: |
    {{ "{" }}Cluster Role: {{ .Resource.Data.metadata.name }}{{ with $rules := .Resource.Data.rules }}{{ "|{" }}Groups|Names|Resources|Verbs{{ "}" }}{{ range $i, $r := $rules }}{{ "|{" }} {{ JoinNicely .apiGroups }}|{{ JoinNicely .resourceNames }}|{{ JoinNicely .resources }}|{{ JoinNicely .verbs }}{{ "}" }} {{end}}{{end}}{{ "}" }}

serviceusage.googleapis.com/Service:
  label: |
    {{ .Resource.Data.name }}
    {{ .Resource.Data.state }}
