Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
sinbai committed Dec 12, 2022
1 parent f04cac2 commit 44a49e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 94 deletions.
104 changes: 20 additions & 84 deletions internal/services/cognitive/cognitive_deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type DeploymentScaleSettingsModel struct {

type CognitiveDeploymentResource struct{}

var _ sdk.ResourceWithUpdate = CognitiveDeploymentResource{}
var _ sdk.Resource = CognitiveDeploymentResource{}

func (r CognitiveDeploymentResource) ResourceType() string {
return "azurerm_cognitive_deployment"
Expand Down Expand Up @@ -66,12 +66,14 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
"model": {
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"format": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"OpenAI",
}, false),
Expand All @@ -87,9 +89,6 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
"version": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"1",
}, false),
},
},
},
Expand All @@ -98,6 +97,7 @@ func (r CognitiveDeploymentResource) Arguments() map[string]*pluginsdk.Schema {
"scale_settings": {
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
Expand Down Expand Up @@ -155,23 +155,13 @@ func (r CognitiveDeploymentResource) Create() sdk.ResourceFunc {
Properties: &deployments.DeploymentProperties{},
}

modelValue, err := expandDeploymentModelModel(model.Model)
if err != nil {
return err
}

properties.Properties.Model = modelValue
properties.Properties.Model = expandDeploymentModelModel(model.Model)

if model.RaiPolicyName != "" {
properties.Properties.RaiPolicyName = &model.RaiPolicyName
}

scaleSettingsValue, err := expandDeploymentScaleSettingsModel(model.ScaleSettings)
if err != nil {
return err
}

properties.Properties.ScaleSettings = scaleSettingsValue
properties.Properties.ScaleSettings = expandDeploymentScaleSettingsModel(model.ScaleSettings)

if err := client.CreateOrUpdateThenPoll(ctx, id, *properties); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
Expand All @@ -183,51 +173,6 @@ func (r CognitiveDeploymentResource) Create() sdk.ResourceFunc {
}
}

func (r CognitiveDeploymentResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Cognitive.DeploymentsClient

id, err := deployments.ParseDeploymentID(metadata.ResourceData.Id())
if err != nil {
return err
}

var model cognitiveDeploymentModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

resp, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

properties := resp.Model
if properties == nil {
return fmt.Errorf("retrieving %s: properties was nil", id)
}

if metadata.ResourceData.HasChange("format") {
properties.Properties.Model.Format = &model.Model[0].Format
}

if metadata.ResourceData.HasChange("model.0.version") {
properties.Properties.Model.Version = &model.Model[0].Version
}

properties.SystemData = nil

if err := client.CreateOrUpdateThenPoll(ctx, *id, *properties); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return nil
},
}
}

func (r CognitiveDeploymentResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Expand Down Expand Up @@ -259,19 +204,10 @@ func (r CognitiveDeploymentResource) Read() sdk.ResourceFunc {
}

if properties := model.Properties; properties != nil {
modelValue, err := flattenDeploymentModelModel(properties.Model)
if err != nil {
return err
}

state.Model = modelValue

scaleSettingsValue, err := flattenDeploymentScaleSettingsModel(properties.ScaleSettings)
if err != nil {
return err
}
state.Model = flattenDeploymentModelModel(properties.Model)

state.ScaleSettings = scaleSettingsValue
state.ScaleSettings = flattenDeploymentScaleSettingsModel(properties.ScaleSettings)

if v := properties.RaiPolicyName; v != nil {
state.RaiPolicyName = *v
Expand Down Expand Up @@ -303,9 +239,9 @@ func (r CognitiveDeploymentResource) Delete() sdk.ResourceFunc {
}
}

func expandDeploymentModelModel(inputList []DeploymentModelModel) (*deployments.DeploymentModel, error) {
func expandDeploymentModelModel(inputList []DeploymentModelModel) *deployments.DeploymentModel {
if len(inputList) == 0 {
return nil, nil
return nil
}

input := &inputList[0]
Expand All @@ -323,26 +259,26 @@ func expandDeploymentModelModel(inputList []DeploymentModelModel) (*deployments.
output.Version = &input.Version
}

return &output, nil
return &output
}

func expandDeploymentScaleSettingsModel(inputList []DeploymentScaleSettingsModel) (*deployments.DeploymentScaleSettings, error) {
func expandDeploymentScaleSettingsModel(inputList []DeploymentScaleSettingsModel) *deployments.DeploymentScaleSettings {
if len(inputList) == 0 {
return nil, nil
return nil
}

input := &inputList[0]
output := deployments.DeploymentScaleSettings{
ScaleType: &input.ScaleType,
}

return &output, nil
return &output
}

func flattenDeploymentModelModel(input *deployments.DeploymentModel) ([]DeploymentModelModel, error) {
func flattenDeploymentModelModel(input *deployments.DeploymentModel) []DeploymentModelModel {
var outputList []DeploymentModelModel
if input == nil {
return outputList, nil
return outputList
}

output := DeploymentModelModel{}
Expand All @@ -364,13 +300,13 @@ func flattenDeploymentModelModel(input *deployments.DeploymentModel) ([]Deployme
}
output.Version = version

return append(outputList, output), nil
return append(outputList, output)
}

func flattenDeploymentScaleSettingsModel(input *deployments.DeploymentScaleSettings) ([]DeploymentScaleSettingsModel, error) {
func flattenDeploymentScaleSettingsModel(input *deployments.DeploymentScaleSettings) []DeploymentScaleSettingsModel {
var outputList []DeploymentScaleSettingsModel
if input == nil {
return outputList, nil
return outputList
}

output := DeploymentScaleSettingsModel{}
Expand All @@ -379,5 +315,5 @@ func flattenDeploymentScaleSettingsModel(input *deployments.DeploymentScaleSetti
output.ScaleType = *input.ScaleType
}

return append(outputList, output), nil
return append(outputList, output)
}
24 changes: 18 additions & 6 deletions internal/services/cognitive/cognitive_deployment_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ import (

type CognitiveDeploymentTestResource struct{}

func TestAccCognitiveDeployment_basic(t *testing.T) {
func TestAccCognitiveDeploymentSequential(t *testing.T) {
// Only two OpenAI resources could be created per region, so run the tests sequentially.
// Refer to : https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quotas-limits
acceptance.RunTestsInSequence(t, map[string]map[string]func(t *testing.T){
"deployment": {
"basic": testAccCognitiveDeployment_basic,
"requiresImport": testAccCognitiveDeployment_requiresImport,
"complete": testAccCognitiveDeployment_complete,
},
})
}

func testAccCognitiveDeployment_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_deployment", "test")
r := CognitiveDeploymentTestResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
Expand All @@ -32,11 +44,11 @@ func TestAccCognitiveDeployment_basic(t *testing.T) {
})
}

func TestAccCognitiveDeployment_requiresImport(t *testing.T) {
func testAccCognitiveDeployment_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_deployment", "test")

r := CognitiveDeploymentTestResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
Expand All @@ -47,10 +59,10 @@ func TestAccCognitiveDeployment_requiresImport(t *testing.T) {
})
}

func TestAccCognitiveDeployment_complete(t *testing.T) {
func testAccCognitiveDeployment_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_deployment", "test")
r := CognitiveDeploymentTestResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
Expand Down
1 change: 0 additions & 1 deletion internal/services/loadbalancer/loadbalancer_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func resourceArmLoadBalancerCreateUpdate(d *pluginsdk.ResourceData, meta interfa

id := parse.NewLoadBalancerID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

log.Printf("elena: is new resource : %t", d.IsNewResource())
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions website/docs/r/cognitive_deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ The following arguments are supported:

A `model` block supports the following:

* `format` - (Required) The format of the Cognitive Services Account Deployment model. Possible value is `OpenAI`.
* `format` - (Required) The format of the Cognitive Services Account Deployment model. Changing this forces a new resource to be created. Possible value is `OpenAI`.

* `name` - (Required) The name of the Cognitive Services Account Deployment model. Changing this forces a new resource to be created.

* `version` - (Required) The version of Cognitive Services Account Deployment model. Possible value is `1`.
* `version` - (Required) The version of Cognitive Services Account Deployment model.

---

A `scale_settings` block supports the following:

* `scale_type` - (Required) Deployment scale type. Possible value is `Standard`.
* `scale_type` - (Required) Deployment scale type. Possible value is `Standard`. Changing this forces a new resource to be created.

## Attributes Reference

Expand Down

0 comments on commit 44a49e1

Please sign in to comment.