@@ -3,7 +3,9 @@ package provider
33import (
44 "strconv"
55 "strings"
6+ "time"
67
8+ "github.com/hashicorp/terraform/helper/resource"
79 "github.com/hashicorp/terraform/helper/schema"
810 "github.com/src-d/terraform-provider-online/online"
911)
@@ -40,6 +42,41 @@ func resourceServer() *schema.Resource {
4042 Elem : resourceInterface (),
4143 Description : "Private interface properties" ,
4244 },
45+ "os_id" : & schema.Schema {
46+ Type : schema .TypeString ,
47+ Required : true ,
48+ ForceNew : true ,
49+ Description : "OS id, use the following command to select one " +
50+ "`curl -s -H \" Authorization: Bearer $ONLINE_TOKEN\" " +
51+ "https://api.online.net/api/v1/server/operatingSystems/{server id}`" ,
52+ },
53+ "user_login" : & schema.Schema {
54+ Type : schema .TypeString ,
55+ Required : true ,
56+ Description : "User login" ,
57+ },
58+ "user_password" : & schema.Schema {
59+ Type : schema .TypeString ,
60+ Required : true ,
61+ Description : "User password" ,
62+ },
63+ "root_password" : & schema.Schema {
64+ Type : schema .TypeString ,
65+ Required : true ,
66+ Description : "Root password" ,
67+ },
68+ "partitioning_template_ref" : & schema.Schema {
69+ Type : schema .TypeString ,
70+ Required : true ,
71+ ForceNew : true ,
72+ Description : "UUID of the partitioning template created from " +
73+ "https://console.online.net/fr/template/partition" ,
74+ },
75+ "status" : & schema.Schema {
76+ Type : schema .TypeString ,
77+ Computed : true ,
78+ Description : "Install status" ,
79+ },
4380 },
4481 }
4582}
@@ -71,9 +108,48 @@ func resourceServerDelete(d *schema.ResourceData, meta interface{}) error {
71108}
72109
73110func resourceServerCreate (d * schema.ResourceData , meta interface {}) error {
111+ c := meta .(online.Client )
112+ s := & online.ServerInstall {
113+ Hostname : d .Get ("hostname" ).(string ),
114+ OS_ID : d .Get ("os_id" ).(string ),
115+ UserLogin : d .Get ("user_login" ).(string ),
116+ UserPassword : d .Get ("user_password" ).(string ),
117+ RootPassword : d .Get ("root_password" ).(string ),
118+ PartitioningTemplateRef : d .Get ("partitioning_template_ref" ).(string ),
119+ }
120+ id := d .Get ("server_id" ).(int )
121+
122+ if err := c .CreateServer (id , s ); err != nil {
123+ return err
124+ }
125+
126+ stateConf := & resource.StateChangeConf {
127+ Pending : []string {"installing" },
128+ Target : []string {"installed" },
129+ Refresh : waitForServerInstall (c , id ),
130+ Timeout : 60 * time .Minute ,
131+ Delay : 1 * time .Second ,
132+ MinTimeout : 10 * time .Second ,
133+ }
134+
135+ _ , err := stateConf .WaitForState ()
136+ if err != nil {
137+ return err
138+ }
139+
74140 return resourceServerRead (d , meta )
75141}
76142
143+ func waitForServerInstall (c online.Client , id int ) resource.StateRefreshFunc {
144+ return func () (interface {}, string , error ) {
145+ s , err := c .Server (id )
146+ if err != nil {
147+ return s , "" , err
148+ }
149+ return s , s .InstallStatus , nil
150+ }
151+ }
152+
77153func resourceServerUpdate (d * schema.ResourceData , meta interface {}) error {
78154 c := meta .(online.Client )
79155 s := & online.Server {
@@ -106,6 +182,7 @@ func resourceServerRead(d *schema.ResourceData, meta interface{}) error {
106182 d .SetId (strconv .Itoa (id ))
107183 d .Set ("hostname" , s .Hostname )
108184 setIP (s , d )
185+ d .Set ("status" , s .InstallStatus )
109186
110187 return nil
111188}
0 commit comments