github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/docker/resource_docker_container.go (about)

     1  package docker
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/terraform/helper/hashcode"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceDockerContainer() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceDockerContainerCreate,
    14  		Read:   resourceDockerContainerRead,
    15  		Update: resourceDockerContainerUpdate,
    16  		Delete: resourceDockerContainerDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			// Indicates whether the container must be running.
    26  			//
    27  			// An assumption is made that configured containers
    28  			// should be running; if not, they should not be in
    29  			// the configuration. Therefore a stopped container
    30  			// should be started. Set to false to have the
    31  			// provider leave the container alone.
    32  			//
    33  			// Actively-debugged containers are likely to be
    34  			// stopped and started manually, and Docker has
    35  			// some provisions for restarting containers that
    36  			// stop. The utility here comes from the fact that
    37  			// this will delete and re-create the container
    38  			// following the principle that the containers
    39  			// should be pristine when started.
    40  			"must_run": &schema.Schema{
    41  				Type:     schema.TypeBool,
    42  				Default:  true,
    43  				Optional: true,
    44  			},
    45  
    46  			// ForceNew is not true for image because we need to
    47  			// sane this against Docker image IDs, as each image
    48  			// can have multiple names/tags attached do it.
    49  			"image": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Required: true,
    52  				ForceNew: true,
    53  			},
    54  
    55  			"hostname": &schema.Schema{
    56  				Type:     schema.TypeString,
    57  				Optional: true,
    58  				ForceNew: true,
    59  			},
    60  
    61  			"domainname": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Optional: true,
    64  				ForceNew: true,
    65  			},
    66  
    67  			"command": &schema.Schema{
    68  				Type:     schema.TypeList,
    69  				Optional: true,
    70  				ForceNew: true,
    71  				Elem:     &schema.Schema{Type: schema.TypeString},
    72  			},
    73  
    74  			"dns": &schema.Schema{
    75  				Type:     schema.TypeSet,
    76  				Optional: true,
    77  				ForceNew: true,
    78  				Elem:     &schema.Schema{Type: schema.TypeString},
    79  				Set:      stringSetHash,
    80  			},
    81  
    82  			"publish_all_ports": &schema.Schema{
    83  				Type:     schema.TypeBool,
    84  				Optional: true,
    85  				ForceNew: true,
    86  			},
    87  
    88  			"volumes": &schema.Schema{
    89  				Type:     schema.TypeSet,
    90  				Optional: true,
    91  				ForceNew: true,
    92  				Elem:     getVolumesElem(),
    93  				Set:      resourceDockerVolumesHash,
    94  			},
    95  
    96  			"ports": &schema.Schema{
    97  				Type:     schema.TypeSet,
    98  				Optional: true,
    99  				ForceNew: true,
   100  				Elem:     getPortsElem(),
   101  				Set:      resourceDockerPortsHash,
   102  			},
   103  
   104  			"env": &schema.Schema{
   105  				Type:     schema.TypeSet,
   106  				Optional: true,
   107  				ForceNew: true,
   108  				Elem:     &schema.Schema{Type: schema.TypeString},
   109  				Set:      stringSetHash,
   110  			},
   111  
   112  			"links": &schema.Schema{
   113  				Type:     schema.TypeSet,
   114  				Optional: true,
   115  				ForceNew: true,
   116  				Elem:     &schema.Schema{Type: schema.TypeString},
   117  				Set:      stringSetHash,
   118  			},
   119  
   120  			"ip_address": &schema.Schema{
   121  				Type:     schema.TypeString,
   122  				Computed: true,
   123  			},
   124  
   125  			"ip_prefix_length": &schema.Schema{
   126  				Type:     schema.TypeInt,
   127  				Computed: true,
   128  			},
   129  
   130  			"gateway": &schema.Schema{
   131  				Type:     schema.TypeString,
   132  				Computed: true,
   133  			},
   134  
   135  			"bridge": &schema.Schema{
   136  				Type:     schema.TypeString,
   137  				Computed: true,
   138  			},
   139  
   140  			"privileged": &schema.Schema{
   141  				Type:     schema.TypeBool,
   142  				Optional: true,
   143  				ForceNew: true,
   144  			},
   145  		},
   146  	}
   147  }
   148  
   149  func getVolumesElem() *schema.Resource {
   150  	return &schema.Resource{
   151  		Schema: map[string]*schema.Schema{
   152  			"from_container": &schema.Schema{
   153  				Type:     schema.TypeString,
   154  				Optional: true,
   155  				ForceNew: true,
   156  			},
   157  
   158  			"container_path": &schema.Schema{
   159  				Type:     schema.TypeString,
   160  				Optional: true,
   161  				ForceNew: true,
   162  			},
   163  
   164  			"host_path": &schema.Schema{
   165  				Type:     schema.TypeString,
   166  				Optional: true,
   167  				ForceNew: true,
   168  			},
   169  
   170  			"read_only": &schema.Schema{
   171  				Type:     schema.TypeBool,
   172  				Optional: true,
   173  				ForceNew: true,
   174  			},
   175  		},
   176  	}
   177  }
   178  
   179  func getPortsElem() *schema.Resource {
   180  	return &schema.Resource{
   181  		Schema: map[string]*schema.Schema{
   182  			"internal": &schema.Schema{
   183  				Type:     schema.TypeInt,
   184  				Required: true,
   185  				ForceNew: true,
   186  			},
   187  
   188  			"external": &schema.Schema{
   189  				Type:     schema.TypeInt,
   190  				Optional: true,
   191  				ForceNew: true,
   192  			},
   193  
   194  			"ip": &schema.Schema{
   195  				Type:     schema.TypeString,
   196  				Optional: true,
   197  				ForceNew: true,
   198  			},
   199  
   200  			"protocol": &schema.Schema{
   201  				Type:     schema.TypeString,
   202  				Default:  "tcp",
   203  				Optional: true,
   204  				ForceNew: true,
   205  			},
   206  		},
   207  	}
   208  }
   209  
   210  func resourceDockerPortsHash(v interface{}) int {
   211  	var buf bytes.Buffer
   212  	m := v.(map[string]interface{})
   213  
   214  	buf.WriteString(fmt.Sprintf("%v-", m["internal"].(int)))
   215  
   216  	if v, ok := m["external"]; ok {
   217  		buf.WriteString(fmt.Sprintf("%v-", v.(int)))
   218  	}
   219  
   220  	if v, ok := m["ip"]; ok {
   221  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   222  	}
   223  
   224  	if v, ok := m["protocol"]; ok {
   225  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   226  	}
   227  
   228  	return hashcode.String(buf.String())
   229  }
   230  
   231  func resourceDockerVolumesHash(v interface{}) int {
   232  	var buf bytes.Buffer
   233  	m := v.(map[string]interface{})
   234  
   235  	if v, ok := m["from_container"]; ok {
   236  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   237  	}
   238  
   239  	if v, ok := m["container_path"]; ok {
   240  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   241  	}
   242  
   243  	if v, ok := m["host_path"]; ok {
   244  		buf.WriteString(fmt.Sprintf("%v-", v.(string)))
   245  	}
   246  
   247  	if v, ok := m["read_only"]; ok {
   248  		buf.WriteString(fmt.Sprintf("%v-", v.(bool)))
   249  	}
   250  
   251  	return hashcode.String(buf.String())
   252  }
   253  
   254  func stringSetHash(v interface{}) int {
   255  	return hashcode.String(v.(string))
   256  }