github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/communicator/shared/shared.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package shared 5 6 import ( 7 "fmt" 8 "net" 9 10 "github.com/terramate-io/tf/configs/configschema" 11 "github.com/zclconf/go-cty/cty" 12 ) 13 14 // ConnectionBlockSupersetSchema is a schema representing the superset of all 15 // possible arguments for "connection" blocks across all supported connection 16 // types. 17 // 18 // This currently lives here because we've not yet updated our communicator 19 // subsystem to be aware of schema itself. Once that is done, we can remove 20 // this and use a type-specific schema from the communicator to validate 21 // exactly what is expected for a given connection type. 22 var ConnectionBlockSupersetSchema = &configschema.Block{ 23 Attributes: map[string]*configschema.Attribute{ 24 // Common attributes for both connection types 25 "host": { 26 Type: cty.String, 27 Required: true, 28 }, 29 "type": { 30 Type: cty.String, 31 Optional: true, 32 }, 33 "user": { 34 Type: cty.String, 35 Optional: true, 36 }, 37 "password": { 38 Type: cty.String, 39 Optional: true, 40 }, 41 "port": { 42 Type: cty.Number, 43 Optional: true, 44 }, 45 "timeout": { 46 Type: cty.String, 47 Optional: true, 48 }, 49 "script_path": { 50 Type: cty.String, 51 Optional: true, 52 }, 53 // For type=ssh only (enforced in ssh communicator) 54 "target_platform": { 55 Type: cty.String, 56 Optional: true, 57 }, 58 "private_key": { 59 Type: cty.String, 60 Optional: true, 61 }, 62 "certificate": { 63 Type: cty.String, 64 Optional: true, 65 }, 66 "host_key": { 67 Type: cty.String, 68 Optional: true, 69 }, 70 "agent": { 71 Type: cty.Bool, 72 Optional: true, 73 }, 74 "agent_identity": { 75 Type: cty.String, 76 Optional: true, 77 }, 78 "proxy_scheme": { 79 Type: cty.String, 80 Optional: true, 81 }, 82 "proxy_host": { 83 Type: cty.String, 84 Optional: true, 85 }, 86 "proxy_port": { 87 Type: cty.Number, 88 Optional: true, 89 }, 90 "proxy_user_name": { 91 Type: cty.String, 92 Optional: true, 93 }, 94 "proxy_user_password": { 95 Type: cty.String, 96 Optional: true, 97 }, 98 "bastion_host": { 99 Type: cty.String, 100 Optional: true, 101 }, 102 "bastion_host_key": { 103 Type: cty.String, 104 Optional: true, 105 }, 106 "bastion_port": { 107 Type: cty.Number, 108 Optional: true, 109 }, 110 "bastion_user": { 111 Type: cty.String, 112 Optional: true, 113 }, 114 "bastion_password": { 115 Type: cty.String, 116 Optional: true, 117 }, 118 "bastion_private_key": { 119 Type: cty.String, 120 Optional: true, 121 }, 122 "bastion_certificate": { 123 Type: cty.String, 124 Optional: true, 125 }, 126 127 // For type=winrm only (enforced in winrm communicator) 128 "https": { 129 Type: cty.Bool, 130 Optional: true, 131 }, 132 "insecure": { 133 Type: cty.Bool, 134 Optional: true, 135 }, 136 "cacert": { 137 Type: cty.String, 138 Optional: true, 139 }, 140 "use_ntlm": { 141 Type: cty.Bool, 142 Optional: true, 143 }, 144 }, 145 } 146 147 // IpFormat formats the IP correctly, so we don't provide IPv6 address in an IPv4 format during node communication. We return the ip parameter as is if it's an IPv4 address or a hostname. 148 func IpFormat(ip string) string { 149 ipObj := net.ParseIP(ip) 150 // Return the ip/host as is if it's either a hostname or an IPv4 address. 151 if ipObj == nil || ipObj.To4() != nil { 152 return ip 153 } 154 155 return fmt.Sprintf("[%s]", ip) 156 }