-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom_commands_project.go
More file actions
186 lines (152 loc) · 5.13 KB
/
custom_commands_project.go
File metadata and controls
186 lines (152 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/google/uuid"
"github.com/hashicorp/terraform/helper/schema"
)
func customCommandsProject() *schema.Resource {
return &schema.Resource{
Create: customCommandsProjectCreate,
Read: customCommandsProjectRead,
Update: customCommandsProjectUpdate,
Delete: customCommandsProjectDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"location": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"custom_commands_speech_key": {
Type: schema.TypeString,
Required: true,
},
"custom_commands_speech_luisa_id": {
Type: schema.TypeString,
Required: true,
},
"custom_commands_speech_luisa_key": {
Type: schema.TypeString,
Required: true,
},
"custom_commands_speech_luisa_location": {
Type: schema.TypeString,
Required: true,
},
"custom_commands_speech_luisp_id": {
Type: schema.TypeString,
Required: true,
},
"custom_commands_speech_luisp_key": {
Type: schema.TypeString,
Required: true,
},
"custom_commands_speech_luisp_location": {
Type: schema.TypeString,
Required: true,
},
"app_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func customCommandsProjectCreate(d *schema.ResourceData, m interface{}) error {
log.Printf("[LOG] Starting Custom Commands Project")
name := d.Get("name").(string)
location := d.Get("location").(string)
basePath := fmt.Sprintf("https://%s.commands.speech.microsoft.com", location)
apiKey := d.Get("custom_commands_speech_key").(string)
type Details struct {
Name string `json:"name"`
Description string `json:"description"`
BaseLanguage string `json:"baseLanguage"`
SkillEnabled bool `json:"skillEnabled"`
}
type LuisResources struct {
AuthoringResourceId string `json:"authoringResourceId"`
AuthoringRegion string `json:"authoringRegion"`
PredictionResourceId string `json:"predictionResourceId"`
PredictionRegion string `json:"predictionRegion"`
}
type DefaultLanguage struct {
LuisResources LuisResources `json:"luisResources"`
DialogModel []string `json:"dialogModel"`
}
type Languages struct {
EnUs DefaultLanguage `json:"en-us"`
}
type DefaultSlot struct {
Languages Languages `json:"languages"`
}
type Slots struct {
Default DefaultSlot `json:"default"`
}
type CustomCommandsAppRequest struct {
Details Details `json:"details"`
Slots Slots `json:"slots"`
}
var customCommandsAppRequest CustomCommandsAppRequest
customCommandsAppRequest.Details.Name = name
customCommandsAppRequest.Details.Description = "New Speech Project"
customCommandsAppRequest.Details.SkillEnabled = true
customCommandsAppRequest.Details.BaseLanguage = "en-us"
customCommandsAppRequest.Slots.Default.Languages.EnUs.LuisResources.AuthoringResourceId = d.Get("custom_commands_speech_luisa_id").(string)
customCommandsAppRequest.Slots.Default.Languages.EnUs.LuisResources.AuthoringRegion = d.Get("custom_commands_speech_luisa_location").(string)
customCommandsAppRequest.Slots.Default.Languages.EnUs.LuisResources.PredictionResourceId = d.Get("custom_commands_speech_luisp_id").(string)
customCommandsAppRequest.Slots.Default.Languages.EnUs.LuisResources.PredictionRegion = d.Get("custom_commands_speech_luisp_location").(string)
customCommandsProjectJSON, errorMessage := json.Marshal(customCommandsAppRequest)
if errorMessage != nil {
return fmt.Errorf("An error happened with json.Marshal: %+v", errorMessage)
}
log.Printf("[LOG] Json object is: %s", string(customCommandsProjectJSON))
appId := uuid.New()
fullPath := fmt.Sprintf("%s/v1.0/apps/%s", basePath, appId)
_, appErrorMessage := submitRequest(http.MethodPut, apiKey, fullPath, customCommandsProjectJSON, 201)
if appErrorMessage != nil {
return fmt.Errorf("An error happened with ioutil.ReadAll: %+v", appErrorMessage)
}
appIdString := fmt.Sprintf("%s", appId)
d.SetId(appIdString)
d.Set("app_id", appIdString)
log.Printf("[LOG] Custom commands project created with app_id: %s", appIdString)
return nil
}
func customCommandsProjectRead(d *schema.ResourceData, m interface{}) error {
return nil
}
func customCommandsProjectUpdate(d *schema.ResourceData, m interface{}) error {
return nil
}
func customCommandsProjectDelete(d *schema.ResourceData, m interface{}) error {
return nil
}
func submitRequest(method string, apiKey string, path string, json []byte, expectedStatusCode int) ([]byte, error) {
var response *http.Response
tries := 0
for true {
innerResponse, errorMessage := CallWebService(path, method, apiKey, expectedStatusCode, json)
if innerResponse.StatusCode == expectedStatusCode {
response = innerResponse
break
} else {
log.Printf("[LOG] Failed attempt %d because of error: %+v", tries, errorMessage)
if errorMessage != nil && tries > 3 {
return nil, fmt.Errorf("An error happened with CallWebService after 3 tries: %+v", errorMessage)
}
}
tries++
time.Sleep(30 * time.Second)
}
return ioutil.ReadAll(response.Body)
}