<commit_before>package ddcloud

import (
	"fmt"
	"log"

	"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
	"github.com/hashicorp/terraform/helper/schema"
)

const (
	resourceKeyNICServerID    = "server"
	resourceKeyNICVLANID      = "vlan"
	resourceKeyNICPrivateIPV4 = "private_ipv4"
	resourceKeyNICPrivateIPV6 = "private_ipv6"
	resourceKeyNICAdapterType = "adapter_type"
)

func resourceServerNIC() *schema.Resource {
	return &schema.Resource{
		Create: resourceServerNICCreate,
		Exists: resourceServerNICExists,
		Read:   resourceServerNICRead,
		Update: resourceServerNICUpdate,
		Delete: resourceServerNICDelete,

		Schema: map[string]*schema.Schema{
			resourceKeyNICServerID: &schema.Schema{
				Type:        schema.TypeString,
				Required:    true,
				Description: "ID of the server to which the additional nics needs to be updated",
			},

			resourceKeyNICVLANID: &schema.Schema{
				Type:        schema.TypeString,
				Computed:    true,
				Optional:    true,
				Description: "VLAN ID of the nic",
				ForceNew:    true,
			},
			resourceKeyNICPrivateIPV4: &schema.Schema{
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "Private IPV4 address for the nic",
			},
			resourceKeyNICPrivateIPV6: &schema.Schema{
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Private IPV6 Address for the nic",
			},
			resourceKeyNICAdapterType: &schema.Schema{
				Type:         schema.TypeString,
				Optional:     true,
				ForceNew:     true,
				Default:      nil,
				Description:  "The type of network adapter (E1000 or VMXNET3)",
				ValidateFunc: validateNICAdapterType,
			},
		},
	}

}

func resourceServerNICCreate(data *schema.ResourceData, provider interface{}) error {
	apiClient := provider.(*providerState).Client()

	propertyHelper := propertyHelper(data)
	serverID := data.Get(resourceKeyNICServerID).(string)
	ipv4Address := data.Get(resourceKeyNICPrivateIPV4).(string)
	vlanID := data.Get(resourceKeyNICVLANID).(string)
	adapterType := propertyHelper.GetOptionalString(resourceKeyNICVLANID, false)

	log.Printf("Configure additional nics for server '%s'...", serverID)

	server, err := apiClient.GetServer(serverID)
	if err != nil {
		return err
	}

	settings := provider.(*providerState).Settings()

	isStarted := server.Started
	if isStarted {
		if settings.AllowServerReboots {
			err = apiClient.ShutdownServer(serverID)
			if err != nil {
				return err
			}

			_, err = apiClient.WaitForChange(compute.ResourceTypeServer, serverID, "Shutdown server", serverShutdownTimeout)
			if err != nil {
				return err
			}
		} else {
			return fmt.Errorf("Cannot reboot server '%s' because server reboots have been disabled via the 'allow_server_reboot' provider setting or 'DDCLOUD_ALLOW_SERVER_REBOOT' environment variable", serverID)
		}
	}
<commit_msg>Fix incorrect behaviour when specifying 'adapter_type' on 'ddcloud_server_nic' (#52).<commit_after>package ddcloud

import (
	"fmt"
	"log"

	"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
	"github.com/hashicorp/terraform/helper/schema"
)

const (
	resourceKeyNICServerID    = "server"
	resourceKeyNICVLANID      = "vlan"
	resourceKeyNICPrivateIPV4 = "private_ipv4"
	resourceKeyNICPrivateIPV6 = "private_ipv6"
	resourceKeyNICAdapterType = "adapter_type"
)

func resourceServerNIC() *schema.Resource {
	return &schema.Resource{
		Create: resourceServerNICCreate,
		Exists: resourceServerNICExists,
		Read:   resourceServerNICRead,
		Update: resourceServerNICUpdate,
		Delete: resourceServerNICDelete,

		Schema: map[string]*schema.Schema{
			resourceKeyNICServerID: &schema.Schema{
				Type:        schema.TypeString,
				Required:    true,
				Description: "ID of the server to which the additional nics needs to be updated",
			},

			resourceKeyNICVLANID: &schema.Schema{
				Type:        schema.TypeString,
				Computed:    true,
				Optional:    true,
				Description: "VLAN ID of the nic",
				ForceNew:    true,
			},
			resourceKeyNICPrivateIPV4: &schema.Schema{
				Type:        schema.TypeString,
				Optional:    true,
				Computed:    true,
				Description: "Private IPV4 address for the nic",
			},
			resourceKeyNICPrivateIPV6: &schema.Schema{
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Private IPV6 Address for the nic",
			},
			resourceKeyNICAdapterType: &schema.Schema{
				Type:         schema.TypeString,
				Optional:     true,
				ForceNew:     true,
				Default:      nil,
				Description:  "The type of network adapter (E1000 or VMXNET3)",
				ValidateFunc: validateNICAdapterType,
			},
		},
	}

}

func resourceServerNICCreate(data *schema.ResourceData, provider interface{}) error {
	apiClient := provider.(*providerState).Client()

	propertyHelper := propertyHelper(data)
	serverID := data.Get(resourceKeyNICServerID).(string)
	ipv4Address := data.Get(resourceKeyNICPrivateIPV4).(string)
	vlanID := data.Get(resourceKeyNICVLANID).(string)
	adapterType := propertyHelper.GetOptionalString(resourceKeyNICAdapterType, false)

	log.Printf("Configure additional nics for server '%s'...", serverID)

	server, err := apiClient.GetServer(serverID)
	if err != nil {
		return err
	}

	settings := provider.(*providerState).Settings()

	isStarted := server.Started
	if isStarted {
		if settings.AllowServerReboots {
			err = apiClient.ShutdownServer(serverID)
			if err != nil {
				return err
			}

			_, err = apiClient.WaitForChange(compute.ResourceTypeServer, serverID, "Shutdown server", serverShutdownTimeout)
			if err != nil {
				return err
			}
		} else {
			return fmt.Errorf("Cannot reboot server '%s' because server reboots have been disabled via the 'allow_server_reboot' provider setting or 'DDCLOUD_ALLOW_SERVER_REBOOT' environment variable", serverID)
		}
	}

{'fraction_non_alphanumeric': 0.10025839793281653, 'fraction_numerical': 0.005512489233419466, 'mean_word_length': 3.953924914675768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7375823', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1832, 'n_words': 412}