# 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: 'Connector'
kind: 'vpcaccess#Connector'
description: 'Serverless VPC Access connector resource.'
references:
  guides:
    'Configuring Serverless VPC Access': 'https://cloud.google.com/vpc/docs/configure-serverless-vpc-access'
  api: 'https://cloud.google.com/vpc/docs/reference/vpcaccess/rest/v1/projects.locations.connectors'
docs:
base_url: 'projects/{{project}}/locations/{{region}}/connectors'
create_url: 'projects/{{project}}/locations/{{region}}/connectors?connectorId={{name}}'
update_url: 'projects/{{project}}/locations/{{region}}/connectors/{{name}}'
update_verb: 'PATCH'
update_mask: true
timeouts:
  insert_minutes: 20
  update_minutes: 20
  delete_minutes: 20
autogen_async: true
async:
  actions: ['create', 'delete', 'update']
  type: 'OpAsync'
  operation:
    base_url: '{{op_id}}'
  result:
    resource_inside_response: true
custom_code:
  encoder: 'templates/terraform/encoders/no_send_name.go.tmpl'
  decoder: 'templates/terraform/decoders/long_name_to_self_link.go.tmpl'
  post_create: 'templates/terraform/post_create/sleep.go.tmpl'
  constants: 'templates/terraform/constants/vpc_access_connector.go.tmpl'
  pre_update: 'templates/terraform/pre_update/vpc_access_connector_instances.go.tmpl'
custom_diff:
  - 'customdiff.ForceNewIfChange("min_instances", isInstanceShrinkage)'
  - 'customdiff.ForceNewIfChange("max_instances", isInstanceShrinkage)'
examples:
  - name: 'vpc_access_connector'
    primary_resource_id: 'connector'
    vars:
      name: 'vpc-con'
      network_name: 'default'
    test_vars_overrides:
      'network_name': 'acctest.BootstrapSharedServiceNetworkingConnection(t, "vpc-access-connector")'
  - name: 'vpc_access_connector_shared_vpc'
    primary_resource_id: 'connector'
    vars:
      name: 'vpc-con'
      network_name: 'default'
    test_vars_overrides:
      'network_name': 'acctest.BootstrapSharedServiceNetworkingConnection(t, "vpc-access-connector")'
parameters:
  - name: 'region'
    type: String
    description: |
      Region where the VPC Access connector resides. If it is not provided, the provider region is used.
    url_param_only: true
    immutable: true
    default_from_api: true
properties:
  - name: 'name'
    type: String
    description: |
      The name of the resource (Max 25 characters).
    required: true
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'
    immutable: true
  - name: 'network'
    type: String
    description: |
      Name or self_link of the VPC network. Required if `ip_cidr_range` is set.
    default_from_api: true
    exactly_one_of:
      - 'network'
      - 'subnet.0.name'
    diff_suppress_func: 'tpgresource.CompareResourceNames'
    custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.tmpl'
    custom_expand: 'templates/terraform/custom_expand/resource_from_self_link.go.tmpl'
    immutable: true
  - name: 'ipCidrRange'
    type: String
    description: |
      The range of internal addresses that follows RFC 4632 notation. Example: `10.132.0.0/28`.
    required_with:
      - 'network'
    immutable: true
  - name: 'state'
    type: Enum
    description: |
      State of the VPC access connector.
    output: true
    enum_values:
      - 'READY'
      - 'CREATING'
      - 'DELETING'
      - 'ERROR'
      - 'UPDATING'
    immutable: true
  - name: 'machineType'
    type: String
    description: |
      Machine type of VM Instance underlying connector. Default is e2-micro
    default_value: "e2-micro"
  - name: 'minThroughput'
    type: Integer
    description: |
      Minimum throughput of the connector in Mbps. Default and min is 200. Refers to the expected throughput when using an e2-micro machine type.
      Value must be a multiple of 100 from 200 through 900. Must be lower than the value specified by max_throughput.
      Only one of `min_throughput` and `min_instances` can be specified. The use of min_throughput is discouraged in favor of min_instances.
    default_from_api: true
    conflicts:
      - min_instances
    validation:
      function: 'validation.IntBetween(200, 1000)'
    immutable: true
  - name: 'minInstances'
    type: Integer
    description: |
      Minimum value of instances in autoscaling group underlying the connector. Value must be between 2 and 9, inclusive. Must be
      lower than the value specified by max_instances.
    default_from_api: true
    conflicts:
      - min_throughput
    required_with:
      - max_instances
  - name: 'maxInstances'
    type: Integer
    description: |
      Maximum value of instances in autoscaling group underlying the connector. Value must be between 3 and 10, inclusive. Must be
      higher than the value specified by min_instances.
    default_from_api: true
    conflicts:
      - max_throughput
    required_with:
      - min_instances
  - name: 'maxThroughput'
    type: Integer
    description: |
      Maximum throughput of the connector in Mbps, must be greater than `min_throughput`. Default is 300. Refers to the expected throughput
      when using an e2-micro machine type. Value must be a multiple of 100 from 300 through 1000. Must be higher than the value specified by
      min_throughput. Only one of `max_throughput` and `max_instances` can be specified. The use of max_throughput is discouraged in favor of max_instances.
    default_from_api: true
    conflicts:
      - max_instances
    validation:
      function: 'validation.IntBetween(200, 1000)'
    immutable: true
  - name: 'selfLink'
    type: String
    description: |
      The fully qualified name of this VPC connector
    ignore_read: true
    output: true
  - name: 'connectedProjects'
    type: Array
    description: |
      List of projects using the connector.
    output: true
    item_type:
      type: String
  - name: 'subnet'
    type: NestedObject
    description: |
      The subnet in which to house the connector
    immutable: true
    properties:
      - name: 'name'
        type: String
        description: |
          Subnet name (relative, not fully qualified). E.g. if the full subnet selfLink is
          https://compute.googleapis.com/compute/v1/projects/{project}/regions/{region}/subnetworks/{subnetName} the correct input for this field would be {subnetName}"
        immutable: true
        exactly_one_of:
          - 'network'
          - 'subnet.0.name'
      - name: 'projectId'
        type: String
        description: |
          Project in which the subnet exists. If not set, this project is assumed to be the project for which the connector create request was issued.
        immutable: true
        default_from_api: true
