<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package manageddisk

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/r3labs/terraform/helper/schema"

	aes "github.com/ernestio/crypto/aes"
	"github.com/ernestio/ernestprovider/event"
	"github.com/ernestio/ernestprovider/providers/azure"
)

// Event : This is the Ernest representation of an azure lb
type Event struct {
	event.Base
	ID                 string            `json:"id"`
	Name               string            `json:"name" validate:"required"`
	ResourceGroupName  string            `json:"resource_group_name" validate:"required"`
	Location           string            `json:"location"`
	StorageAccountType string            `json:"storage_account_type"`
	CreateOption       string            `json:"create_option"`
	SourceURI          string            `json:"source_uri"`
	SourceResourceID   string            `json:"source_resource_id"`
	OSType             string            `json:"os_type"`
	DiskSizeGB         int32             `json:"disk_size_gb"`
	Tags               map[string]string `json:"tags"`
	ClientID           string            `json:"azure_client_id"`
	ClientSecret       string            `json:"azure_client_secret"`
	TenantID           string            `json:"azure_tenant_id"`
	SubscriptionID     string            `json:"azure_subscription_id"`
	Environment        string            `json:"environment"`
	ErrorMessage       string            `json:"error,omitempty"`
	Components         []json.RawMessage `json:"components"`
	CryptoKey          string            `json:"-"`
	Validator          *event.Validator  `json:"-"`
}

// New : Constructor
func New(subject, cryptoKey string, body []byte, val *event.Validator) (event.Event, error) {
	var ev event.Resource
	ev = &Event{CryptoKey: cryptoKey, Validator: val}
	body = []byte(strings.Replace(string(body), `"_component":"managed_disks"`, `"_component":"managed_disk"`, 1))
	if err := json.Unmarshal(body, &ev); err != nil {
		err := fmt.Errorf("Error on input message : %s", err)
		return nil, err
	}

	return azure.New(subject, "azurerm_managed_disk", body, val, ev)
}

// SetComponents : ....
func (ev *Event) SetComponents(components []event.Event) {
	for _, v := range components {
		ev.Components = append(ev.Components, v.GetBody())
	}
}

// ValidateID : determines if the given id is valid for this resource type
func (ev *Event) ValidateID(id string) bool {
	parts := strings.Split(strings.ToLower(id), "/")
	if len(parts) != 9 {
		return false
	}
	if parts[6] != "microsoft.compute" {
		return false
	}
	if parts[7] != "disks" {
		return false
	}
	return true
}

// SetID : id setter
func (ev *Event) SetID(id string) {
	ev.ID = id
}

// GetID : id getter
func (ev *Event) GetID() string {
	return ev.ID
}

// SetState : state setter
func (ev *Event) SetState(state string) {
	ev.State = state
}

// ResourceDataToEvent : Translates a ResourceData on a valid Ernest Event
func (ev *Event) ResourceDataToEvent(d *schema.ResourceData) error {
	idParts := strings.Split(d.Id(), "/")
	ev.ID = d.Id()
	ev.Name = idParts[len(idParts)-1]
	ev.ComponentID = "managed_disk::" + ev.Name
	ev.ResourceGroupName = d.Get("resource_group_name").(string)
	ev.Location = d.Get("location").(string)
	ev.StorageAccountType = d.Get("storage_account_type").(string)
	ev.CreateOption = d.Get("create_option").(string)
	ev.SourceURI = d.Get("source_uri").(string)
	ev.SourceResourceID = d.Get("source_resource_id").(string)
	ev.OSType = d.Get("os_type").(string)
	ev.DiskSizeGB = int32(d.Get("disk_size_gb").(int))
	tags := make(map[string]string, 0)
	for k, v := range d.Get("tags").(map[string]interface{}) {
		tags[k] = v.(string)
	}
	ev.Tags = tags
<commit_msg>Only fill resource group if is not set<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package manageddisk

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/r3labs/terraform/helper/schema"

	aes "github.com/ernestio/crypto/aes"
	"github.com/ernestio/ernestprovider/event"
	"github.com/ernestio/ernestprovider/providers/azure"
)

// Event : This is the Ernest representation of an azure lb
type Event struct {
	event.Base
	ID                 string            `json:"id"`
	Name               string            `json:"name" validate:"required"`
	ResourceGroupName  string            `json:"resource_group_name" validate:"required"`
	Location           string            `json:"location"`
	StorageAccountType string            `json:"storage_account_type"`
	CreateOption       string            `json:"create_option"`
	SourceURI          string            `json:"source_uri"`
	SourceResourceID   string            `json:"source_resource_id"`
	OSType             string            `json:"os_type"`
	DiskSizeGB         int32             `json:"disk_size_gb"`
	Tags               map[string]string `json:"tags"`
	ClientID           string            `json:"azure_client_id"`
	ClientSecret       string            `json:"azure_client_secret"`
	TenantID           string            `json:"azure_tenant_id"`
	SubscriptionID     string            `json:"azure_subscription_id"`
	Environment        string            `json:"environment"`
	ErrorMessage       string            `json:"error,omitempty"`
	Components         []json.RawMessage `json:"components"`
	CryptoKey          string            `json:"-"`
	Validator          *event.Validator  `json:"-"`
}

// New : Constructor
func New(subject, cryptoKey string, body []byte, val *event.Validator) (event.Event, error) {
	var ev event.Resource
	ev = &Event{CryptoKey: cryptoKey, Validator: val}
	body = []byte(strings.Replace(string(body), `"_component":"managed_disks"`, `"_component":"managed_disk"`, 1))
	if err := json.Unmarshal(body, &ev); err != nil {
		err := fmt.Errorf("Error on input message : %s", err)
		return nil, err
	}

	return azure.New(subject, "azurerm_managed_disk", body, val, ev)
}

// SetComponents : ....
func (ev *Event) SetComponents(components []event.Event) {
	for _, v := range components {
		ev.Components = append(ev.Components, v.GetBody())
	}
}

// ValidateID : determines if the given id is valid for this resource type
func (ev *Event) ValidateID(id string) bool {
	parts := strings.Split(strings.ToLower(id), "/")
	if len(parts) != 9 {
		return false
	}
	if parts[6] != "microsoft.compute" {
		return false
	}
	if parts[7] != "disks" {
		return false
	}
	return true
}

// SetID : id setter
func (ev *Event) SetID(id string) {
	ev.ID = id
}

// GetID : id getter
func (ev *Event) GetID() string {
	return ev.ID
}

// SetState : state setter
func (ev *Event) SetState(state string) {
	ev.State = state
}

// ResourceDataToEvent : Translates a ResourceData on a valid Ernest Event
func (ev *Event) ResourceDataToEvent(d *schema.ResourceData) error {
	idParts := strings.Split(d.Id(), "/")
	ev.ID = d.Id()
	ev.Name = idParts[len(idParts)-1]
	ev.ComponentID = "managed_disk::" + ev.Name
	if ev.ResourceGroupName == "" {
		ev.ResourceGroupName = d.Get("resource_group_name").(string)
	}
	ev.Location = d.Get("location").(string)
	ev.StorageAccountType = d.Get("storage_account_type").(string)
	ev.CreateOption = d.Get("create_option").(string)
	ev.SourceURI = d.Get("source_uri").(string)
	ev.SourceResourceID = d.Get("source_resource_id").(string)
	ev.OSType = d.Get("os_type").(string)
	ev.DiskSizeGB = int32(d.Get("disk_size_gb").(int))
	tags := make(map[string]string, 0)
	for k, v := range d.Get("tags").(map[string]interface{}) {
		tags[k] = v.(string)
	}
	ev.Tags = tags

{'fraction_non_alphanumeric': 0.13752883875929248, 'fraction_numerical': 0.003845167905665214, 'mean_word_length': 3.115506329113924, 'pattern_counts': {'":': 4, '<': 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': '2476483', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2486, 'n_words': 611}