# Copyright 2024 Google Inc.
# 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.

---
name: 'Address'
kind: 'compute#address'
description: |
  Represents an Address resource.

  Each virtual machine instance has an ephemeral internal IP address and,
  optionally, an external IP address. To communicate between instances on
  the same network, you can use an instance's internal IP address. To
  communicate with the Internet and instances outside of the same network,
  you must specify the instance's external IP address.

  Internal IP addresses are ephemeral and only belong to an instance for
  the lifetime of the instance; if the instance is deleted and recreated,
  the instance is assigned a new internal IP address, either by Compute
  Engine or by you. External IP addresses can be either ephemeral or
  static.
references:
  guides:
    'Reserving a Static External IP Address': 'https://cloud.google.com/compute/docs/instances-and-network'
    'Reserving a Static Internal IP Address': 'https://cloud.google.com/compute/docs/ip-addresses/reserve-static-internal-ip-address'
  api: 'https://cloud.google.com/compute/docs/reference/beta/addresses'
docs:
base_url: 'projects/{{project}}/regions/{{region}}/addresses'
has_self_link: true
immutable: true
timeouts:
  insert_minutes: 20
  update_minutes: 20
  delete_minutes: 20
async:
  actions: ['create', 'delete', 'update']
  type: 'OpAsync'
  operation:
    base_url: '{{op_id}}'
  result:
    resource_inside_response: false
collection_url_key: 'items'
custom_code:
  post_create: 'templates/terraform/post_create/labels.tmpl'
sweeper:
  url_substitutions:
    - region: "us-west2"
    - region: "us-central1"
    - region: "us-east1"
    - region: "europe-west1"
    - region: "us-west1"
examples:
  - name: 'address_basic'
    primary_resource_id: 'ip_address'
    vars:
      address_name: 'my-address'
  - name: 'address_with_subnetwork'
    primary_resource_id: 'internal_with_subnet_and_address'
    vars:
      address_name: 'my-internal-address'
      network_name: 'my-network'
      subnetwork_name: 'my-subnet'
  - name: 'address_with_gce_endpoint'
    primary_resource_id: 'internal_with_gce_endpoint'
    vars:
      address_name: 'my-internal-address-'
  - name: 'address_with_shared_loadbalancer_vip'
    primary_resource_id: 'internal_with_shared_loadbalancer_vip'
    vars:
      address_name: 'my-internal-address'
 # It is almost identical to internal_with_gce_endpoint
    exclude_docs: true
  # TODO(rileykarson): Remove this example when instance is supported
  - name: 'instance_with_ip'
    primary_resource_id: 'static'
    vars:
      address_name: 'ipv4-address'
      instance_name: 'vm-instance'
  - name: 'compute_address_ipsec_interconnect'
    primary_resource_id: 'ipsec-interconnect-address'
    vars:
      address_name: 'test-address'
      network_name: 'test-network'
parameters:
  - name: 'region'
    type: ResourceRef
    description: |
      The Region in which the created address should reside.
      If it is not provided, the provider region is used.
    required: false
    default_from_api: true
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'
    custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.tmpl'
    resource: 'Region'
    imports: 'name'
properties:
  - name: 'address'
    type: String
    description: |
      The static external IP address represented by this resource.
      The IP address must be inside the specified subnetwork,
      if any. Set by the API if undefined.
    default_from_api: true
  - name: 'addressType'
    type: Enum
    description: |
      The type of address to reserve.
      Note: if you set this argument's value as `INTERNAL` you need to leave the `network_tier` argument unset in that resource block.
    custom_flatten: 'templates/terraform/custom_flatten/default_if_empty.tmpl'
    default_value: "EXTERNAL"
    enum_values:
      - 'INTERNAL'
      - 'EXTERNAL'
  - name: 'creationTimestamp'
    type: Time
    description: 'Creation timestamp in RFC3339 text format.'
    output: true
  - name: 'description'
    type: String
    description: 'An optional description of this resource.'
  - name: 'name'
    type: String
    description: |
      Name of the resource. The name must be 1-63 characters long, and
      comply with RFC1035. Specifically, the name must be 1-63 characters
      long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?`
      which means the first character must be a lowercase letter, and all
      following characters must be a dash, lowercase letter, or digit,
      except the last character, which cannot be a dash.
    required: true
    validation:
      regex: '^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$'
  - name: 'purpose'
    type: String
    description: |
      The purpose of this resource, which can be one of the following values.

      * GCE_ENDPOINT for addresses that are used by VM instances, alias IP
      ranges, load balancers, and similar resources.

      * SHARED_LOADBALANCER_VIP for an address that can be used by multiple
      internal load balancers.

      * VPC_PEERING for addresses that are reserved for VPC peer networks.

      * IPSEC_INTERCONNECT for addresses created from a private IP range that
      are reserved for a VLAN attachment in an HA VPN over Cloud Interconnect
      configuration. These addresses are regional resources.

      * PRIVATE_SERVICE_CONNECT for a private network address that is used to
      configure Private Service Connect. Only global internal addresses can use
      this purpose.

      This should only be set when using an Internal address.
    default_from_api: true
  - name: 'networkTier'
    type: Enum
    description: |
      The networking tier used for configuring this address. If this field is not
      specified, it is assumed to be PREMIUM.
      This argument should not be used when configuring Internal addresses, because [network tier cannot be set for internal traffic; it's always Premium](https://cloud.google.com/network-tiers/docs/overview).
    default_from_api: true
    enum_values:
      - 'PREMIUM'
      - 'STANDARD'
  - name: 'subnetwork'
    type: ResourceRef
    description: |
      The URL of the subnetwork in which to reserve the address. If an IP
      address is specified, it must be within the subnetwork's IP range.
      This field can only be used with INTERNAL type with
      GCE_ENDPOINT/DNS_RESOLVER purposes.
    default_from_api: true
    custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.tmpl'
    resource: 'Subnetwork'
    imports: 'selfLink'
  - name: 'users'
    type: Array
    description: 'The URLs of the resources that are using this address.'
    output: true
    item_type:
      type: String
  - name: 'labels'
    type: KeyValueLabels
    description: |
      Labels to apply to this address.  A list of key->value pairs.
    update_url: 'projects/{{project}}/regions/{{region}}/addresses/{{name}}/setLabels'
    update_verb: 'POST'
  - name: 'labelFingerprint'
    type: Fingerprint
    description: |
      The fingerprint used for optimistic locking of this resource.  Used
      internally during updates.
    output: true
    update_url: 'projects/{{project}}/regions/{{region}}/addresses/{{name}}/setLabels'
    update_verb: 'POST'
    key_expander: ''
  - name: 'network'
    type: ResourceRef
    description: |
      The URL of the network in which to reserve the address. This field
      can only be used with INTERNAL type with the VPC_PEERING and
      IPSEC_INTERCONNECT purposes.
    custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.tmpl'
    resource: 'Network'
    imports: 'selfLink'
  - name: 'prefixLength'
    type: Integer
    description: |
      The prefix length if the resource represents an IP range.
    default_from_api: true
  - name: 'ipVersion'
    type: Enum
    description: |
      The IP Version that will be used by this address. The default value is `IPV4`.
    diff_suppress_func: 'tpgresource.EmptyOrDefaultStringSuppress("IPV4")'
    enum_values:
      - 'IPV4'
      - 'IPV6'
  - name: 'ipv6EndpointType'
    type: Enum
    description: |
      The endpoint type of this address, which should be VM or NETLB. This is
      used for deciding which type of endpoint this address can be used after
      the external IPv6 address reservation.
    enum_values:
      - 'VM'
      - 'NETLB'
