<commit_before>package opennebula

import (
	"encoding/xml"
	"fmt"
	"github.com/hashicorp/terraform/helper/schema"
	"log"
	"strconv"
	"strings"
)

type UserVnets struct {
	UserVnet []*UserVnet `xml:"VNET"`
}

type UserVnet struct {
	Name        string       `xml:"NAME"`
	Id          int          `xml:"ID"`
	Uid         int          `xml:"UID"`
	Gid         int          `xml:"GID"`
	Uname       string       `xml:"UNAME"`
	Gname       string       `xml:"GNAME"`
	Permissions *Permissions `xml:"PERMISSIONS"`
	Bridge      string       `xml:"BRIDGE"`
}

func resourceVnet() *schema.Resource {
	return &schema.Resource{
		Create: resourceVnetCreate,
		Read:   resourceVnetRead,
		Exists: resourceVnetExists,
		Update: resourceVnetUpdate,
		Delete: resourceVnetDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"name": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Name of the vnet",
			},
			"description": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Description of the vnet, in OpenNebula's XML or String format",
			},
			"permissions": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Permissions for the vnet (in Unix format, owner-group-other, use-manage-admin)",
				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
					value := v.(string)

					if len(value) != 3 {
						errors = append(errors, fmt.Errorf("%q has specify 3 permission sets: owner-group-other", k))
					}

					all := true
					for _, c := range strings.Split(value, "") {
						if c < "0" || c > "7" {
							all = false
						}
					}
					if !all {
						errors = append(errors, fmt.Errorf("Each character in %q should specify a Unix-like permission set with a number from 0 to 7", k))
					}

					return
				},
			},

			"uid": {
				Type:        schema.TypeInt,
				Computed:    true,
				Description: "ID of the user that will own the vnet",
			},
			"gid": {
				Type:        schema.TypeInt,
				Computed:    true,
				Description: "ID of the group that will own the vnet",
			},
			"uname": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Name of the user that will own the vnet",
			},
			"gname": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Name of the group that will own the vnet",
			},
			"bridge": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Name of the bridge interface to which the vnet should be associated",
			},
			"ip_start": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Start IP of the range to be allocated",
			},
			"ip_size": {
				Type:        schema.TypeInt,
				Required:    true,
				Description: "Size (in number) of the ip range",
			},
			"reservation_size": {
				Type:        schema.TypeInt,
				Optional:    true,
				Description: "Carve a network reservation of this size from the reservation starting from `ip-start`",
			},
		},
	}
}

func resourceVnetCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*Client)
	// Create base object
	resp, err := client.Call(
		"one.vn.allocate",
		fmt.Sprintf("NAME = \"%s\"\n", d.Get("name").(string))+d.Get("description").(string)+"\nBRIDGE="+d.Get("bridge").(string),
		-1,
	)
	if err != nil {
		return err
	}

	d.SetId(resp)
	// update permisions
	if _, err = changePermissions(intId(d.Id()), permission(d.Get("permissions").(string)), client, "one.vn.chmod"); err != nil {
		return err
	}
	// add address range and reservations
	var address_range_string = `AR = [
  TYPE = IP4,
  IP = %s,
  SIZE = %d ]`
	_, a_err := client.Call(
		"one.vn.add_ar",
		intId(d.Id()),
		fmt.Sprintf(address_range_string, d.Get("ip_start").(string), d.Get("ip_size").(int)),
	)

	if a_err != nil {
		return a_err
	}

	if d.Get("reservation_size").(int) > 0 {
		// add address range and reservations
		var address_reservation_string = `SIZE = %d
    NAME = ASDF`
		_, r_err := client.Call(
			"one.vn.reserve",
			intId(d.Id()),
			fmt.Sprintf(address_reservation_string, d.Get("reservation_size").(int)),
		)

		if r_err != nil {
			return r_err
		}

	}

	return resourceVnetRead(d, meta)<commit_msg>changed name of address reservation<commit_after>package opennebula

import (
	"encoding/xml"
	"fmt"
	"github.com/hashicorp/terraform/helper/schema"
	"log"
	"strconv"
	"strings"
)

type UserVnets struct {
	UserVnet []*UserVnet `xml:"VNET"`
}

type UserVnet struct {
	Name        string       `xml:"NAME"`
	Id          int          `xml:"ID"`
	Uid         int          `xml:"UID"`
	Gid         int          `xml:"GID"`
	Uname       string       `xml:"UNAME"`
	Gname       string       `xml:"GNAME"`
	Permissions *Permissions `xml:"PERMISSIONS"`
	Bridge      string       `xml:"BRIDGE"`
}

func resourceVnet() *schema.Resource {
	return &schema.Resource{
		Create: resourceVnetCreate,
		Read:   resourceVnetRead,
		Exists: resourceVnetExists,
		Update: resourceVnetUpdate,
		Delete: resourceVnetDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"name": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Name of the vnet",
			},
			"description": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Description of the vnet, in OpenNebula's XML or String format",
			},
			"permissions": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Permissions for the vnet (in Unix format, owner-group-other, use-manage-admin)",
				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
					value := v.(string)

					if len(value) != 3 {
						errors = append(errors, fmt.Errorf("%q has specify 3 permission sets: owner-group-other", k))
					}

					all := true
					for _, c := range strings.Split(value, "") {
						if c < "0" || c > "7" {
							all = false
						}
					}
					if !all {
						errors = append(errors, fmt.Errorf("Each character in %q should specify a Unix-like permission set with a number from 0 to 7", k))
					}

					return
				},
			},

			"uid": {
				Type:        schema.TypeInt,
				Computed:    true,
				Description: "ID of the user that will own the vnet",
			},
			"gid": {
				Type:        schema.TypeInt,
				Computed:    true,
				Description: "ID of the group that will own the vnet",
			},
			"uname": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Name of the user that will own the vnet",
			},
			"gname": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "Name of the group that will own the vnet",
			},
			"bridge": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Name of the bridge interface to which the vnet should be associated",
			},
			"ip_start": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "Start IP of the range to be allocated",
			},
			"ip_size": {
				Type:        schema.TypeInt,
				Required:    true,
				Description: "Size (in number) of the ip range",
			},
			"reservation_size": {
				Type:        schema.TypeInt,
				Optional:    true,
				Description: "Carve a network reservation of this size from the reservation starting from `ip-start`",
			},
		},
	}
}

func resourceVnetCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*Client)
	// Create base object
	resp, err := client.Call(
		"one.vn.allocate",
		fmt.Sprintf("NAME = \"%s\"\n", d.Get("name").(string))+d.Get("description").(string)+"\nBRIDGE="+d.Get("bridge").(string),
		-1,
	)
	if err != nil {
		return err
	}

	d.SetId(resp)
	// update permisions
	if _, err = changePermissions(intId(d.Id()), permission(d.Get("permissions").(string)), client, "one.vn.chmod"); err != nil {
		return err
	}
	// add address range and reservations
	var address_range_string = `AR = [
  TYPE = IP4,
  IP = %s,
  SIZE = %d ]`
	_, a_err := client.Call(
		"one.vn.add_ar",
		intId(d.Id()),
		fmt.Sprintf(address_range_string, d.Get("ip_start").(string), d.Get("ip_size").(int)),
	)

	if a_err != nil {
		return a_err
	}

	if d.Get("reservation_size").(int) > 0 {
		// add address range and reservations
		var address_reservation_string = `SIZE = %d
    NAME = %s`
		_, r_err := client.Call(
			"one.vn.reserve",
			intId(d.Id()),
			fmt.Sprintf(address_reservation_string, d.Get("reservation_size").(int)),
      d.Get("name").(string)+"-reserved",
		)

		if r_err != nil {
			return r_err
		}

	}

	return resourceVnetRead(d, meta)
{'fraction_non_alphanumeric': 0.14025374855824682, 'fraction_numerical': 0.0020761245674740486, 'mean_word_length': 2.6601941747572817, 'pattern_counts': {'":': 22, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '5973201', 'n_tokens_mistral': 3451, 'n_tokens_neox': 2895, 'n_words': 762}