github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/communicator/shared/shared.go (about)

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  
     7  	"github.com/iaas-resource-provision/iaas-rpc/internal/configs/configschema"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  // ConnectionBlockSupersetSchema is a schema representing the superset of all
    12  // possible arguments for "connection" blocks across all supported connection
    13  // types.
    14  //
    15  // This currently lives here because we've not yet updated our communicator
    16  // subsystem to be aware of schema itself. Once that is done, we can remove
    17  // this and use a type-specific schema from the communicator to validate
    18  // exactly what is expected for a given connection type.
    19  var ConnectionBlockSupersetSchema = &configschema.Block{
    20  	Attributes: map[string]*configschema.Attribute{
    21  		// Common attributes for both connection types
    22  		"host": {
    23  			Type:     cty.String,
    24  			Required: true,
    25  		},
    26  		"type": {
    27  			Type:     cty.String,
    28  			Optional: true,
    29  		},
    30  		"user": {
    31  			Type:     cty.String,
    32  			Optional: true,
    33  		},
    34  		"password": {
    35  			Type:     cty.String,
    36  			Optional: true,
    37  		},
    38  		"port": {
    39  			Type:     cty.Number,
    40  			Optional: true,
    41  		},
    42  		"timeout": {
    43  			Type:     cty.String,
    44  			Optional: true,
    45  		},
    46  		"script_path": {
    47  			Type:     cty.String,
    48  			Optional: true,
    49  		},
    50  		// For type=ssh only (enforced in ssh communicator)
    51  		"target_platform": {
    52  			Type:     cty.String,
    53  			Optional: true,
    54  		},
    55  		"private_key": {
    56  			Type:     cty.String,
    57  			Optional: true,
    58  		},
    59  		"certificate": {
    60  			Type:     cty.String,
    61  			Optional: true,
    62  		},
    63  		"host_key": {
    64  			Type:     cty.String,
    65  			Optional: true,
    66  		},
    67  		"agent": {
    68  			Type:     cty.Bool,
    69  			Optional: true,
    70  		},
    71  		"agent_identity": {
    72  			Type:     cty.String,
    73  			Optional: true,
    74  		},
    75  		"bastion_host": {
    76  			Type:     cty.String,
    77  			Optional: true,
    78  		},
    79  		"bastion_host_key": {
    80  			Type:     cty.String,
    81  			Optional: true,
    82  		},
    83  		"bastion_port": {
    84  			Type:     cty.Number,
    85  			Optional: true,
    86  		},
    87  		"bastion_user": {
    88  			Type:     cty.String,
    89  			Optional: true,
    90  		},
    91  		"bastion_password": {
    92  			Type:     cty.String,
    93  			Optional: true,
    94  		},
    95  		"bastion_private_key": {
    96  			Type:     cty.String,
    97  			Optional: true,
    98  		},
    99  		"bastion_certificate": {
   100  			Type:     cty.String,
   101  			Optional: true,
   102  		},
   103  
   104  		// For type=winrm only (enforced in winrm communicator)
   105  		"https": {
   106  			Type:     cty.Bool,
   107  			Optional: true,
   108  		},
   109  		"insecure": {
   110  			Type:     cty.Bool,
   111  			Optional: true,
   112  		},
   113  		"cacert": {
   114  			Type:     cty.String,
   115  			Optional: true,
   116  		},
   117  		"use_ntlm": {
   118  			Type:     cty.Bool,
   119  			Optional: true,
   120  		},
   121  	},
   122  }
   123  
   124  // 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.
   125  func IpFormat(ip string) string {
   126  	ipObj := net.ParseIP(ip)
   127  	// Return the ip/host as is if it's either a hostname or an IPv4 address.
   128  	if ipObj == nil || ipObj.To4() != nil {
   129  		return ip
   130  	}
   131  
   132  	return fmt.Sprintf("[%s]", ip)
   133  }