55 "fmt"
66 "io"
77 "net/http"
8+ "strconv"
9+ "strings"
810
911 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012)
@@ -15,11 +17,27 @@ func resourceDockerNetwork() *schema.Resource {
1517 Read : resourceDockerNetworkRead ,
1618 Delete : resourceDockerNetworkDelete ,
1719 Update : nil ,
20+ Importer : & schema.ResourceImporter {
21+ State : func (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
22+ // Expect ID in format "<endpoint_id>:<network_id>"
23+ parts := strings .SplitN (d .Id (), ":" , 2 )
24+ if len (parts ) != 2 {
25+ return nil , fmt .Errorf ("unexpected format of ID (%q), expected <endpoint_id>:<network_id>" , d .Id ())
26+ }
27+ endpointID , err := strconv .Atoi (parts [0 ])
28+ if err != nil {
29+ return nil , fmt .Errorf ("invalid endpoint ID: %w" , err )
30+ }
31+ d .Set ("endpoint_id" , endpointID )
32+ d .SetId (parts [1 ])
33+ return []* schema.ResourceData {d }, nil
34+ },
35+ },
1836 Schema : map [string ]* schema.Schema {
1937 "endpoint_id" : {Type : schema .TypeInt , Required : true , ForceNew : true },
2038 "name" : {Type : schema .TypeString , Required : true , ForceNew : true },
2139 "driver" : {Type : schema .TypeString , Optional : true , Default : "bridge" , ForceNew : true },
22- "scope" : {Type : schema .TypeString , Optional : true , ForceNew : true },
40+ "scope" : {Type : schema .TypeString , Optional : true , Default : "local" , ForceNew : true },
2341 "internal" : {Type : schema .TypeBool , Optional : true , Default : false , ForceNew : true },
2442 "attachable" : {Type : schema .TypeBool , Optional : true , Default : false , ForceNew : true },
2543 "ingress" : {Type : schema .TypeBool , Optional : true , Default : false , ForceNew : true },
@@ -163,112 +181,79 @@ func resourceDockerNetworkCreate(d *schema.ResourceData, meta interface{}) error
163181}
164182
165183func resourceDockerNetworkRead (d * schema.ResourceData , meta interface {}) error {
184+ client := meta .(* APIClient )
185+ endpointID := d .Get ("endpoint_id" ).(int )
186+ networkID := d .Id ()
187+ path := fmt .Sprintf ("/endpoints/%d/docker/networks/%s" , endpointID , networkID )
188+ resp , err := client .DoRequest (http .MethodGet , path , nil , nil )
189+ if err != nil {
190+ return fmt .Errorf ("failed to read docker network: %w" , err )
191+ }
192+ defer resp .Body .Close ()
193+
194+ if resp .StatusCode == 404 {
195+ d .SetId ("" )
196+ return nil
197+ }
198+ if resp .StatusCode != 200 {
199+ body , _ := io .ReadAll (resp .Body )
200+ return fmt .Errorf ("failed to read docker network: %s" , string (body ))
201+ }
202+
203+ var result struct {
204+ Name string `json:"Name"`
205+ Driver string `json:"Driver"`
206+ Scope string `json:"Scope"`
207+ Internal bool `json:"Internal"`
208+ Attachable bool `json:"Attachable"`
209+ Ingress bool `json:"Ingress"`
210+ ConfigOnly bool `json:"ConfigOnly"`
211+ EnableIPv4 bool `json:"EnableIPv4"`
212+ EnableIPv6 bool `json:"EnableIPv6"`
213+ Options map [string ]interface {} `json:"Options"`
214+ Labels map [string ]string `json:"Labels"`
215+ IPAM struct {
216+ Driver string `json:"Driver"`
217+ Options map [string ]string `json:"Options"`
218+ Config []map [string ]interface {} `json:"Config"`
219+ } `json:"IPAM"`
220+ }
221+
222+ if err := json .NewDecoder (resp .Body ).Decode (& result ); err != nil {
223+ return fmt .Errorf ("failed to decode docker network response: %w" , err )
224+ }
225+
226+ _ = d .Set ("name" , result .Name )
227+ _ = d .Set ("driver" , result .Driver )
228+ _ = d .Set ("scope" , result .Scope )
229+ _ = d .Set ("internal" , result .Internal )
230+ _ = d .Set ("attachable" , result .Attachable )
231+ _ = d .Set ("ingress" , result .Ingress )
232+ _ = d .Set ("config_only" , result .ConfigOnly )
233+ _ = d .Set ("enable_ipv4" , result .EnableIPv4 )
234+ _ = d .Set ("enable_ipv6" , result .EnableIPv6 )
235+ _ = d .Set ("options" , result .Options )
236+
237+ labels := make (map [string ]interface {}, len (result .Labels ))
238+ for k , v := range result .Labels {
239+ labels [k ] = v
240+ }
241+ _ = d .Set ("labels" , labels )
242+
243+ // IPAM config
244+ _ = d .Set ("ipam_driver" , result .IPAM .Driver )
245+
246+ ipamOpts := make (map [string ]interface {}, len (result .IPAM .Options ))
247+ for k , v := range result .IPAM .Options {
248+ ipamOpts [k ] = v
249+ }
250+ _ = d .Set ("ipam_options" , ipamOpts )
251+
252+ _ = d .Set ("ipam_config" , result .IPAM .Config )
253+
166254 return nil
167255}
168256
169- // Use if will be exists PUT (update) methods for Docker Netwerks over API
170- ///func resourceDockerNetworkRead(d *schema.ResourceData, meta interface{}) error {
171- /// client := meta.(*APIClient)
172- /// endpointID := d.Get("endpoint_id").(int)
173- /// networkID := d.Id()
174- ///
175- /// path := fmt.Sprintf("/endpoints/%d/docker/networks/%s", endpointID, networkID)
176- /// resp, err := client.DoRequest(http.MethodGet, path, nil, nil)
177- /// if err != nil {
178- /// return fmt.Errorf("failed to read docker network: %w", err)
179- /// }
180- /// defer resp.Body.Close()
181- ///
182- /// if resp.StatusCode == 404 {
183- /// d.SetId("")
184- /// return nil
185- /// } else if resp.StatusCode != 200 {
186- /// body, _ := io.ReadAll(resp.Body)
187- /// return fmt.Errorf("failed to read docker network: %s", string(body))
188- /// }
189- ///
190- /// var network struct {
191- /// ID string `json:"Id"`
192- /// Name string `json:"Name"`
193- /// Driver string `json:"Driver"`
194- /// Scope string `json:"Scope"`
195- /// Internal bool `json:"Internal"`
196- /// Attachable bool `json:"Attachable"`
197- /// Ingress bool `json:"Ingress"`
198- /// ConfigOnly bool `json:"ConfigOnly"`
199- /// ConfigFrom map[string]string `json:"ConfigFrom"`
200- /// EnableIPv4 *bool `json:"EnableIPv4,omitempty"`
201- /// EnableIPv6 *bool `json:"EnableIPv6,omitempty"`
202- /// IPAM struct {
203- /// Driver string `json:"Driver"`
204- /// Options map[string]string `json:"Options"`
205- /// Config []map[string]interface{} `json:"Config"`
206- /// } `json:"IPAM"`
207- /// Options map[string]string `json:"Options"`
208- /// Labels map[string]string `json:"Labels"`
209- /// }
210- ///
211- /// if err := json.NewDecoder(resp.Body).Decode(&network); err != nil {
212- /// return err
213- /// }
214- ///
215- /// d.Set("name", network.Name)
216- /// d.Set("driver", network.Driver)
217- /// d.Set("scope", network.Scope)
218- /// d.Set("internal", network.Internal)
219- /// d.Set("attachable", network.Attachable)
220- /// d.Set("ingress", network.Ingress)
221- /// d.Set("config_only", network.ConfigOnly)
222- ///
223- /// if network.EnableIPv4 != nil {
224- /// d.Set("enable_ipv4", *network.EnableIPv4)
225- /// }
226- /// if network.EnableIPv6 != nil {
227- /// d.Set("enable_ipv6", *network.EnableIPv6)
228- /// }
229- ///
230- /// if v, ok := network.ConfigFrom["Network"]; ok {
231- /// d.Set("config_from", v)
232- /// }
233- ///
234- /// d.Set("ipam_driver", network.IPAM.Driver)
235- ///
236- /// if network.IPAM.Options != nil {
237- /// d.Set("ipam_options", network.IPAM.Options)
238- /// } else {
239- /// d.Set("ipam_options", map[string]string{})
240- /// }
241- ///
242- /// var ipamConfigList []map[string]interface{}
243- /// for _, c := range network.IPAM.Config {
244- /// entry := map[string]interface{}{}
245- /// if subnet, ok := c["Subnet"]; ok {
246- /// entry["subnet"] = subnet
247- /// }
248- /// if ipRange, ok := c["IPRange"]; ok {
249- /// entry["ip_range"] = ipRange
250- /// }
251- /// if gateway, ok := c["Gateway"]; ok {
252- /// entry["gateway"] = gateway
253- /// }
254- /// if aux, ok := c["AuxiliaryAddresses"]; ok {
255- /// entry["auxiliary_addresses"] = aux
256- /// }
257- /// ipamConfigList = append(ipamConfigList, entry)
258- /// }
259- ///
260- /// if ipamConfigList != nil {
261- /// d.Set("ipam_config", ipamConfigList)
262- /// } else {
263- /// d.Set("ipam_config", []interface{}{})
264- /// }
265- ///
266- /// d.Set("options", network.Options)
267- /// d.Set("labels", network.Labels)
268- ///
269- /// return nil
270- ///
271-
272257func resourceDockerNetworkDelete (d * schema.ResourceData , meta interface {}) error {
273258 client := meta .(* APIClient )
274259 endpointID := d .Get ("endpoint_id" ).(int )
0 commit comments