mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
parent
93e38c8fa8
commit
b71103b0f2
@ -1,17 +1,13 @@
|
|||||||
package profitbricks
|
package profitbricks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/x509"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/pem"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/profitbricks/profitbricks-sdk-go"
|
"github.com/profitbricks/profitbricks-sdk-go"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -147,8 +143,8 @@ func resourceProfitBricksServer() *schema.Resource {
|
|||||||
Elem: &schema.Schema{Type: schema.TypeString},
|
Elem: &schema.Schema{Type: schema.TypeString},
|
||||||
Computed: true,
|
Computed: true,
|
||||||
},
|
},
|
||||||
"nat": {
|
"nat" :{
|
||||||
Type: schema.TypeBool,
|
Type: schema.TypeBool,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
},
|
||||||
"firewall_active": {
|
"firewall_active": {
|
||||||
@ -258,18 +254,19 @@ func resourceProfitBricksServerCreate(d *schema.ResourceData, meta interface{})
|
|||||||
var sshkey_path []interface{}
|
var sshkey_path []interface{}
|
||||||
var image, licenceType, availabilityZone string
|
var image, licenceType, availabilityZone string
|
||||||
|
|
||||||
if rawMap["image_name"] != nil {
|
if !IsValidUUID(rawMap["image_name"].(string)) {
|
||||||
if !IsValidUUID(rawMap["image_name"].(string)) {
|
if rawMap["image_name"] != nil {
|
||||||
image = getImageId(d.Get("datacenter_id").(string), rawMap["image_name"].(string), rawMap["disk_type"].(string))
|
image = getImageId(d.Get("datacenter_id").(string), rawMap["image_name"].(string), rawMap["disk_type"].(string))
|
||||||
if image == "" {
|
if image == "" {
|
||||||
dc := profitbricks.GetDatacenter(d.Get("datacenter_id").(string))
|
dc := profitbricks.GetDatacenter(d.Get("datacenter_id").(string))
|
||||||
return fmt.Errorf("Image '%s' doesn't exist. in location %s", rawMap["image_name"], dc.Properties.Location)
|
return fmt.Errorf("Image '%s' doesn't exist. in location %s", rawMap["image_name"], dc.Properties.Location)
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
image = rawMap["image_name"].(string)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
image = rawMap["image_name"].(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rawMap["licence_type"] != nil {
|
if rawMap["licence_type"] != nil {
|
||||||
licenceType = rawMap["licence_type"].(string)
|
licenceType = rawMap["licence_type"].(string)
|
||||||
}
|
}
|
||||||
@ -291,7 +288,7 @@ func resourceProfitBricksServerCreate(d *schema.ResourceData, meta interface{})
|
|||||||
log.Printf("[DEBUG] Reading file %s", path)
|
log.Printf("[DEBUG] Reading file %s", path)
|
||||||
publicKey, err := readPublicKey(path.(string))
|
publicKey, err := readPublicKey(path.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error fetching sshkey from file (%s) (%s)", path, err.Error())
|
return fmt.Errorf("Error fetching sshkey from file (%s) %s", path, err.Error())
|
||||||
}
|
}
|
||||||
publicKeys = append(publicKeys, publicKey)
|
publicKeys = append(publicKeys, publicKey)
|
||||||
}
|
}
|
||||||
@ -308,13 +305,13 @@ func resourceProfitBricksServerCreate(d *schema.ResourceData, meta interface{})
|
|||||||
Items: []profitbricks.Volume{
|
Items: []profitbricks.Volume{
|
||||||
{
|
{
|
||||||
Properties: profitbricks.VolumeProperties{
|
Properties: profitbricks.VolumeProperties{
|
||||||
Name: rawMap["name"].(string),
|
Name: rawMap["name"].(string),
|
||||||
Size: rawMap["size"].(int),
|
Size: rawMap["size"].(int),
|
||||||
Type: rawMap["disk_type"].(string),
|
Type: rawMap["disk_type"].(string),
|
||||||
ImagePassword: imagePassword,
|
ImagePassword: imagePassword,
|
||||||
Image: image,
|
Image: image,
|
||||||
Bus: rawMap["bus"].(string),
|
Bus: rawMap["bus"].(string),
|
||||||
LicenceType: licenceType,
|
LicenceType: licenceType,
|
||||||
AvailabilityZone: availabilityZone,
|
AvailabilityZone: availabilityZone,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -403,7 +400,7 @@ func resourceProfitBricksServerCreate(d *schema.ResourceData, meta interface{})
|
|||||||
}
|
}
|
||||||
|
|
||||||
request.Entities.Nics.Items[0].Entities = &profitbricks.NicEntities{
|
request.Entities.Nics.Items[0].Entities = &profitbricks.NicEntities{
|
||||||
Firewallrules: &profitbricks.FirewallRules{
|
Firewallrules : &profitbricks.FirewallRules{
|
||||||
Items: []profitbricks.FirewallRule{
|
Items: []profitbricks.FirewallRule{
|
||||||
firewall,
|
firewall,
|
||||||
},
|
},
|
||||||
@ -435,7 +432,8 @@ func resourceProfitBricksServerCreate(d *schema.ResourceData, meta interface{})
|
|||||||
}
|
}
|
||||||
d.SetId(server.Id)
|
d.SetId(server.Id)
|
||||||
server = profitbricks.GetServer(d.Get("datacenter_id").(string), server.Id)
|
server = profitbricks.GetServer(d.Get("datacenter_id").(string), server.Id)
|
||||||
d.Set("primary_nic", server.Entities.Nics.Items[0])
|
|
||||||
|
d.Set("primary_nic", server.Entities.Nics.Items[0].Id)
|
||||||
if len(server.Entities.Nics.Items[0].Properties.Ips) > 0 {
|
if len(server.Entities.Nics.Items[0].Properties.Ips) > 0 {
|
||||||
d.SetConnInfo(map[string]string{
|
d.SetConnInfo(map[string]string{
|
||||||
"type": "ssh",
|
"type": "ssh",
|
||||||
@ -448,25 +446,10 @@ func resourceProfitBricksServerCreate(d *schema.ResourceData, meta interface{})
|
|||||||
|
|
||||||
func resourceProfitBricksServerRead(d *schema.ResourceData, meta interface{}) error {
|
func resourceProfitBricksServerRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
dcId := d.Get("datacenter_id").(string)
|
dcId := d.Get("datacenter_id").(string)
|
||||||
|
serverId := d.Id()
|
||||||
|
|
||||||
server := profitbricks.GetServer(dcId, d.Id())
|
server := profitbricks.GetServer(dcId, serverId)
|
||||||
|
primarynic := d.Get("primary_nic").(string)
|
||||||
primarynic := ""
|
|
||||||
|
|
||||||
if server.Entities != nil && server.Entities.Nics != nil && len(server.Entities.Nics.Items) > 0 {
|
|
||||||
for _, n := range server.Entities.Nics.Items {
|
|
||||||
if n.Properties.Lan != 0 {
|
|
||||||
lan := profitbricks.GetLan(dcId, strconv.Itoa(n.Properties.Lan))
|
|
||||||
if lan.StatusCode > 299 {
|
|
||||||
return fmt.Errorf("Error while fetching a lan %s", lan.Response)
|
|
||||||
}
|
|
||||||
if lan.Properties.Public.(interface{}) == true {
|
|
||||||
primarynic = n.Id
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
d.Set("name", server.Properties.Name)
|
d.Set("name", server.Properties.Name)
|
||||||
d.Set("cores", server.Properties.Cores)
|
d.Set("cores", server.Properties.Cores)
|
||||||
@ -474,6 +457,31 @@ func resourceProfitBricksServerRead(d *schema.ResourceData, meta interface{}) er
|
|||||||
d.Set("availability_zone", server.Properties.AvailabilityZone)
|
d.Set("availability_zone", server.Properties.AvailabilityZone)
|
||||||
d.Set("primary_nic", primarynic)
|
d.Set("primary_nic", primarynic)
|
||||||
|
|
||||||
|
nic := profitbricks.GetNic(dcId, serverId, primarynic)
|
||||||
|
|
||||||
|
if len(nic.Properties.Ips) > 0 {
|
||||||
|
d.Set("primary_ip", nic.Properties.Ips[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
if nRaw, ok := d.GetOk("nic"); ok {
|
||||||
|
log.Printf("[DEBUG] parsing nic")
|
||||||
|
|
||||||
|
nicRaw := nRaw.(*schema.Set).List()
|
||||||
|
|
||||||
|
for _, raw := range nicRaw {
|
||||||
|
|
||||||
|
rawMap := raw.(map[string]interface{})
|
||||||
|
|
||||||
|
rawMap["lan"] = nic.Properties.Lan
|
||||||
|
rawMap["name"] = nic.Properties.Name
|
||||||
|
rawMap["dhcp"] = nic.Properties.Dhcp
|
||||||
|
rawMap["nat"] = nic.Properties.Nat
|
||||||
|
rawMap["firewall_active"] = nic.Properties.FirewallActive
|
||||||
|
rawMap["ips"] = nic.Properties.Ips
|
||||||
|
}
|
||||||
|
d.Set("nic", nicRaw)
|
||||||
|
}
|
||||||
|
|
||||||
if server.Properties.BootVolume != nil {
|
if server.Properties.BootVolume != nil {
|
||||||
d.Set("boot_volume", server.Properties.BootVolume.Id)
|
d.Set("boot_volume", server.Properties.BootVolume.Id)
|
||||||
}
|
}
|
||||||
@ -638,38 +646,4 @@ func readPublicKey(path string) (key string, err error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return string(ssh.MarshalAuthorizedKey(pubKey)[:]), nil
|
return string(ssh.MarshalAuthorizedKey(pubKey)[:]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSshKey(d *schema.ResourceData, path string) (privatekey string, publickey string, err error) {
|
|
||||||
pemBytes, err := ioutil.ReadFile(path)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
block, _ := pem.Decode(pemBytes)
|
|
||||||
|
|
||||||
if block == nil {
|
|
||||||
return "", "", errors.New("File " + path + " contains nothing")
|
|
||||||
}
|
|
||||||
|
|
||||||
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
priv_blk := pem.Block{
|
|
||||||
Type: "RSA PRIVATE KEY",
|
|
||||||
Headers: nil,
|
|
||||||
Bytes: x509.MarshalPKCS1PrivateKey(priv),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub, err := ssh.NewPublicKey(&priv.PublicKey)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
publickey = string(ssh.MarshalAuthorizedKey(pub))
|
|
||||||
privatekey = string(pem.EncodeToMemory(&priv_blk))
|
|
||||||
|
|
||||||
return privatekey, publickey, nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user