4
4
"encoding/json"
5
5
"errors"
6
6
"io/ioutil"
7
- "os"
8
7
9
8
"github.com/google/uuid"
10
9
consul "github.com/hashicorp/consul/api"
@@ -13,10 +12,20 @@ import (
13
12
packet "github.com/packethost/packngo"
14
13
)
15
14
16
- var (
17
- // PacketProjectID is the packet project id where instances should be created. Figure this out...
18
- PacketProjectID = os .Getenv ("PACKET_PROJECT_ID" )
19
- )
15
+ // GetPacketProjectFromAuthPayload returns the Packet project of a project level API key
16
+ func GetPacketProjectFromAuthPayload (auth string ) (string , error ) {
17
+ packetClient := packet .NewClientWithAuth ("" , auth , nil )
18
+ var project map [string ]interface {}
19
+ _ , err := packetClient .DoRequest ("GET" , "/project" , "" , & project )
20
+ if err != nil {
21
+ return "" , err
22
+ }
23
+ projectID , ok := project ["id" ]
24
+ if ! ok {
25
+ return "" , errors .New ("problem verifying project from auth" )
26
+ }
27
+ return projectID .(string ), nil
28
+ }
20
29
21
30
// GetPacketInstance gets an instance by ID
22
31
func GetPacketInstance (consulClient * consul.Client , in * pb.GetInstanceRequest ) (* pb.Instance , error ) {
@@ -38,14 +47,12 @@ func CreatePacketInstance(consulClient *consul.Client, in *pb.CreateInstanceRequ
38
47
id := uuid .New ()
39
48
40
49
packetClient := packet .NewClientWithAuth ("" , in .Auth .Payload , nil )
41
- _ , _ , err := packetClient .Users .Current ()
50
+
51
+ projID , err := GetPacketProjectFromAuthPayload (in .Auth .Payload )
42
52
if err != nil {
43
53
return nil , err
44
54
}
45
55
46
- // TODO: figure this out...
47
- projID := PacketProjectID
48
-
49
56
instance , err := instance .CreateInstance (consulClient , instance.CreateInstanceRequest {
50
57
ID : id .String (),
51
58
Owner : projID ,
@@ -88,7 +95,7 @@ func CreatePacketInstance(consulClient *consul.Client, in *pb.CreateInstanceRequ
88
95
Hostname : "open-copilot-instance-" + id .String (),
89
96
ProjectID : projID ,
90
97
Facility : "ewr1" ,
91
- Plan : "baremetal_2 " ,
98
+ Plan : "baremetal_1 " ,
92
99
OS : "ubuntu_16_04" ,
93
100
BillingCycle : "hourly" ,
94
101
CustomData : string (customDataJSON ),
0 commit comments