@@ -34,6 +34,62 @@ func TestAccTritonMachine_basic(t *testing.T) {
3434 })
3535}
3636
37+ func TestAccTritonMachine_nic (t * testing.T ) {
38+ machineName := fmt .Sprintf ("acctest-%d" , acctest .RandInt ())
39+ config := fmt .Sprintf (testAccTritonMachine_withnic , machineName , machineName )
40+
41+ resource .Test (t , resource.TestCase {
42+ PreCheck : func () { testAccPreCheck (t ) },
43+ Providers : testAccProviders ,
44+ CheckDestroy : testCheckTritonMachineDestroy ,
45+ Steps : []resource.TestStep {
46+ resource.TestStep {
47+ Config : config ,
48+ Check : resource .ComposeTestCheckFunc (
49+ testCheckTritonMachineExists ("triton_machine.test" ),
50+ func (* terraform.State ) error {
51+ time .Sleep (10 * time .Second )
52+ return nil
53+ },
54+ testCheckTritonMachineHasFabric ("triton_machine.test" , "triton_fabric.test" ),
55+ ),
56+ },
57+ },
58+ })
59+ }
60+
61+ func TestAccTritonMachine_addnic (t * testing.T ) {
62+ machineName := fmt .Sprintf ("acctest-%d" , acctest .RandInt ())
63+ without := fmt .Sprintf (testAccTritonMachine_withoutnic , machineName , machineName )
64+ with := fmt .Sprintf (testAccTritonMachine_withnic , machineName , machineName )
65+
66+ resource .Test (t , resource.TestCase {
67+ PreCheck : func () { testAccPreCheck (t ) },
68+ Providers : testAccProviders ,
69+ CheckDestroy : testCheckTritonMachineDestroy ,
70+ Steps : []resource.TestStep {
71+ resource.TestStep {
72+ Config : without ,
73+ Check : resource .ComposeTestCheckFunc (
74+ testCheckTritonMachineExists ("triton_machine.test" ),
75+ func (* terraform.State ) error {
76+ time .Sleep (10 * time .Second )
77+ return nil
78+ },
79+ testCheckTritonMachineHasNoFabric ("triton_machine.test" , "triton_fabric.test" ),
80+ ),
81+ },
82+ resource.TestStep {
83+ Config : with ,
84+ Check : resource .ComposeTestCheckFunc (
85+ testCheckTritonMachineExists ("triton_machine.test" ),
86+ testCheckTritonMachineHasFabric ("triton_machine.test" , "triton_fabric.test" ),
87+ ),
88+ },
89+ },
90+ })
91+ }
92+
3793func testCheckTritonMachineExists (name string ) resource.TestCheckFunc {
3894 return func (s * terraform.State ) error {
3995 // Ensure we have enough information in state to look up in API
@@ -56,6 +112,66 @@ func testCheckTritonMachineExists(name string) resource.TestCheckFunc {
56112 }
57113}
58114
115+ func testCheckTritonMachineHasFabric (name , fabricName string ) resource.TestCheckFunc {
116+ return func (s * terraform.State ) error {
117+ // Ensure we have enough information in state to look up in API
118+ machine , ok := s .RootModule ().Resources [name ]
119+ if ! ok {
120+ return fmt .Errorf ("Not found: %s" , name )
121+ }
122+
123+ network , ok := s .RootModule ().Resources [fabricName ]
124+ if ! ok {
125+ return fmt .Errorf ("Not found: %s" , fabricName )
126+ }
127+ conn := testAccProvider .Meta ().(* cloudapi.Client )
128+
129+ nics , err := conn .ListNICs (machine .Primary .ID )
130+ if err != nil {
131+ return fmt .Errorf ("Bad: Check NICs Exist: %s" , err )
132+ }
133+
134+ fmt .Printf ("%+v\n " , machine .Primary )
135+ for _ , nic := range nics {
136+ fmt .Printf ("---\n %+v\n %+v\n " , nic , network .Primary )
137+ if nic .Network == network .Primary .ID {
138+ return nil
139+ }
140+ }
141+
142+ return fmt .Errorf ("Bad: Machine %q does not have Fabric %q" , machine .Primary .ID , network .Primary .ID )
143+ }
144+ }
145+
146+ func testCheckTritonMachineHasNoFabric (name , fabricName string ) resource.TestCheckFunc {
147+ return func (s * terraform.State ) error {
148+ // Ensure we have enough information in state to look up in API
149+ machine , ok := s .RootModule ().Resources [name ]
150+ if ! ok {
151+ return fmt .Errorf ("Not found: %s" , name )
152+ }
153+
154+ network , ok := s .RootModule ().Resources [fabricName ]
155+ if ! ok {
156+ return fmt .Errorf ("Not found: %s" , fabricName )
157+ }
158+ conn := testAccProvider .Meta ().(* cloudapi.Client )
159+
160+ nics , err := conn .ListNICs (machine .Primary .ID )
161+ if err != nil {
162+ return fmt .Errorf ("Bad: Check NICs Exist: %s" , err )
163+ }
164+
165+ for _ , nic := range nics {
166+ if nic .Network == network .Primary .ID {
167+ return fmt .Errorf ("Bad: Machine %q has Fabric %q" , machine .Primary .ID , network .Primary .ID )
168+ }
169+ }
170+
171+ return nil
172+ }
173+ }
174+
59175func testCheckTritonMachineDestroy (s * terraform.State ) error {
60176 conn := testAccProvider .Meta ().(* cloudapi.Client )
61177
@@ -84,7 +200,59 @@ resource "triton_machine" "test" {
84200 image = "842e6fa6-6e9b-11e5-8402-1b490459e334"
85201
86202 tags = {
87- test = "hello!"
88- }
203+ test = "hello!"
204+ }
205+ }
206+ `
207+
208+ var testAccTritonMachine_withnic = `
209+ resource "triton_fabric" "test" {
210+ name = "%s-network"
211+ description = "test network"
212+ vlan_id = 2 # every DC seems to have a vlan 2 available
213+
214+ subnet = "10.0.0.0/22"
215+ gateway = "10.0.0.1"
216+ provision_start_ip = "10.0.0.5"
217+ provision_end_ip = "10.0.3.250"
218+
219+ resolvers = ["8.8.8.8", "8.8.4.4"]
220+ }
221+
222+ resource "triton_machine" "test" {
223+ name = "%s"
224+ package = "g3-standard-0.25-smartos"
225+ image = "842e6fa6-6e9b-11e5-8402-1b490459e334"
226+
227+ tags = {
228+ test = "hello!"
229+ }
230+
231+ nic { network = "${triton_fabric.test.id}" }
232+ }
233+ `
234+
235+ var testAccTritonMachine_withoutnic = `
236+ resource "triton_fabric" "test" {
237+ name = "%s-network"
238+ description = "test network"
239+ vlan_id = 2 # every DC seems to have a vlan 2 available
240+
241+ subnet = "10.0.0.0/22"
242+ gateway = "10.0.0.1"
243+ provision_start_ip = "10.0.0.5"
244+ provision_end_ip = "10.0.3.250"
245+
246+ resolvers = ["8.8.8.8", "8.8.4.4"]
247+ }
248+
249+ resource "triton_machine" "test" {
250+ name = "%s"
251+ package = "g3-standard-0.25-smartos"
252+ image = "842e6fa6-6e9b-11e5-8402-1b490459e334"
253+
254+ tags = {
255+ test = "hello!"
256+ }
89257}
90258`
0 commit comments