<commit_before>package softlayer

import (
	"errors"
	"fmt"

	"github.com/hashicorp/terraform/helper/schema"
	"github.com/softlayer/softlayer-go/datatypes"
	"github.com/softlayer/softlayer-go/filter"
	"github.com/softlayer/softlayer-go/services"
	"github.com/softlayer/softlayer-go/session"
)

func dataSourceSoftLayerVlan() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceSoftLayerVlanRead,

		Schema: map[string]*schema.Schema{
			"id": {
				Type:     schema.TypeInt,
				Computed: true,
			},

			"name": {
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},

			"number": {
				Type:     schema.TypeInt,
				Optional: true,
				Computed: true,
			},

			"router_hostname": {
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},

			"subnets": {
				Type:     schema.TypeList,
				Computed: true,
				Elem:     &schema.Schema{Type: schema.TypeString},
			},
		},
	}
}

func dataSourceSoftLayerVlanRead(d *schema.ResourceData, meta interface{}) error {
	sess := meta.(*session.Session)
	service := services.GetAccountService(sess)

	name := d.Get("name").(string)
	number := d.Get("number").(int)
	routerHostname := d.Get("router_hostname").(string)
	var vlan *datatypes.Network_Vlan
	var err error

	if number != 0 && routerHostname != "" {
		// Got vlan number and router, get vlan, and compute name
		vlan, err = getVlan(number, routerHostname, meta)
		if err != nil {
			return err
		}

		d.SetId(fmt.Sprintf("%d", *vlan.Id))
		if vlan.Name != nil {
			d.Set("name", *vlan.Name)
		}
	} else if name != "" {
		// Got name, get vlan, and compute router hostname and vlan number
		networkVlans, err := service.
			Mask("id,vlanNumber,name,primaryRouter[hostname],primarySubnets[networkIdentifier,cidr]").
			Filter(filter.Path("networkVlans.name").Eq(name).Build()).
			GetNetworkVlans()
		if err != nil {
			return fmt.Errorf("Error obtaining VLAN id: %s", err)
		} else if len(networkVlans) == 0 {
			return fmt.Errorf("No VLAN was found with the name '%s'", name)
		}

		vlan = &networkVlans[0]
		d.SetId(fmt.Sprintf("%d", *vlan.Id))
		d.Set("number", *vlan.VlanNumber)

		if vlan.PrimaryRouter != nil {
			d.Set("router_hostname", *vlan.PrimaryRouter.Hostname)
		}
	} else {
		return errors.New("Missing required properties. Need a VLAN name, or the VLAN's number and router hostname.")
	}

	// Get subnets in cidr format for display
	if len(vlan.PrimarySubnets) > 0 {
		subnets := make([]string, len(vlan.PrimarySubnets))
		for i, subnet := range vlan.PrimarySubnets {
			subnets[i] = fmt.Sprintf("%s/%d", *subnet.NetworkIdentifier, *subnet.Cidr)
		}

		d.Set("subnets", subnets)
	}

	return nil
}<commit_msg>Hostname is not necessarily present on a returned VLAN<commit_after>package softlayer

import (
	"errors"
	"fmt"

	"github.com/hashicorp/terraform/helper/schema"
	"github.com/softlayer/softlayer-go/datatypes"
	"github.com/softlayer/softlayer-go/filter"
	"github.com/softlayer/softlayer-go/services"
	"github.com/softlayer/softlayer-go/session"
)

func dataSourceSoftLayerVlan() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceSoftLayerVlanRead,

		Schema: map[string]*schema.Schema{
			"id": {
				Type:     schema.TypeInt,
				Computed: true,
			},

			"name": {
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},

			"number": {
				Type:     schema.TypeInt,
				Optional: true,
				Computed: true,
			},

			"router_hostname": {
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},

			"subnets": {
				Type:     schema.TypeList,
				Computed: true,
				Elem:     &schema.Schema{Type: schema.TypeString},
			},
		},
	}
}

func dataSourceSoftLayerVlanRead(d *schema.ResourceData, meta interface{}) error {
	sess := meta.(*session.Session)
	service := services.GetAccountService(sess)

	name := d.Get("name").(string)
	number := d.Get("number").(int)
	routerHostname := d.Get("router_hostname").(string)
	var vlan *datatypes.Network_Vlan
	var err error

	if number != 0 && routerHostname != "" {
		// Got vlan number and router, get vlan, and compute name
		vlan, err = getVlan(number, routerHostname, meta)
		if err != nil {
			return err
		}

		d.SetId(fmt.Sprintf("%d", *vlan.Id))
		if vlan.Name != nil {
			d.Set("name", *vlan.Name)
		}
	} else if name != "" {
		// Got name, get vlan, and compute router hostname and vlan number
		networkVlans, err := service.
			Mask("id,vlanNumber,name,primaryRouter[hostname],primarySubnets[networkIdentifier,cidr]").
			Filter(filter.Path("networkVlans.name").Eq(name).Build()).
			GetNetworkVlans()
		if err != nil {
			return fmt.Errorf("Error obtaining VLAN id: %s", err)
		} else if len(networkVlans) == 0 {
			return fmt.Errorf("No VLAN was found with the name '%s'", name)
		}

		vlan = &networkVlans[0]
		d.SetId(fmt.Sprintf("%d", *vlan.Id))
		d.Set("number", *vlan.VlanNumber)

		if vlan.PrimaryRouter != nil && vlan.PrimaryRouter.Hostname != nil {
			d.Set("router_hostname", *vlan.PrimaryRouter.Hostname)
		}
	} else {
		return errors.New("Missing required properties. Need a VLAN name, or the VLAN's number and router hostname.")
	}

	// Get subnets in cidr format for display
	if len(vlan.PrimarySubnets) > 0 {
		subnets := make([]string, len(vlan.PrimarySubnets))
		for i, subnet := range vlan.PrimarySubnets {
			subnets[i] = fmt.Sprintf("%s/%d", *subnet.NetworkIdentifier, *subnet.Cidr)
		}

		d.Set("subnets", subnets)
	}

	return nil
}
{'fraction_non_alphanumeric': 0.14896349293707575, 'fraction_numerical': 0.001467620620069712, 'mean_word_length': 3.6321155480033984, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, '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': '4215009', 'n_tokens_mistral': 2190, 'n_tokens_neox': 1940, 'n_words': 416}