尝试通过Azure REST API创建VM时出现运行时错误

时间:2018-01-10 17:50:57

标签: python rest api azure azure-management-api

我一直关注these instructions 使用python在Azure上创建VM。但是,Azure返回错误:



<!DOCTYPE html>
<html>
    <head>
        <title>Runtime Error</title>
        <meta name=viewport content=width=device-width />
        <style>
         body {font-family:Verdana;font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:Verdana;font-weight:normal;color:black;margin-         b {font-family:Verdana;font-weight:bold;color:black;margin-         H1 { font-family:Verdana;font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:Verdana;font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:Consolas,Lucida Console,Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
         @media screen and (max-width: 639px) {
          pre { width: 440px;  white-space: pre-wrap; word-wrap: break-word; }
         }
         @media screen and (max-width: 479px) {
          pre { width: 280px; }
         }
        </style>
    </head>

    <body bgcolor=white>

            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i>Runtime Error</i> </h2></span>

            <font face=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif >

            <b> Description: </b>An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
            <br><br>

            <b>Details:</b> To enable the details of this specific error message to be viewable on remote machines, please create a &lt;customErrors&gt; tag within a &quot;web.config&quot; configuration file located in the root directory of the current web application. This &lt;customErrors&gt; tag should then have its &quot;mode&quot; attribute set to &quot;Off&quot;.<br><br>

            <table width=100% bgcolor=#ffffcc>
               <tr>
                  <td>
                      <code><pre>

&lt;!-- Web.Config Configuration File --&gt;
&lt;configuration&gt;
    &lt;system.web&gt;
        &lt;customErrors mode=&quot;Off&quot;/&gt;
    &lt;/system.web&gt;
&lt;/configuration&gt;</pre></code>

                  </td>
               </tr>
            </table>

            <br>

            <b>Notes:</b> The current error page you are seeing can be replaced by a custom error page by modifying the &quot;defaultRedirect&quot; attribute of the application&#39;s &lt;customErrors&gt; configuration tag to point to a custom error page URL.<br><br>

            <table width=100% bgcolor=#ffffcc>
               <tr>
                  <td>
                      <code><pre>

&lt;!-- Web.Config Configuration File --&gt;

&lt;configuration&gt;
    &lt;system.web&gt;
        &lt;customErrors mode=&quot;RemoteOnly&quot; defaultRedirect=&quot;mycustompage.htm&quot;/&gt;
    &lt;/system.web&gt;
&lt;/configuration&gt;</pre></code>

                  </td>
               </tr>
            </table>

            <br>

    </body>
</html>
&#13;
&#13;
&#13;

令我困惑的部分是我没有通过任何类型的Web应用程序与API交互,我在本地运行这个python脚本,并且它(理想情况下)在没有用户的情况下运行交互一旦开始。所以我不确定我是如何修改任何网络配置文件以获得更有用的错误,除非它引用我的Azure帐户上的某些内容,在这种情况下,我不是确定它指的是什么。

我已使用OAUTH2令牌身份验证以编程方式进行身份验证,之前允许我使用相同的python脚本创建/更新资源组,虚拟网络等(我实际上是在创建VM)。此外,在使用REST API创建资源组和虚拟网络时,它返回了有用的错误,详细说明了出错的地方。所以我完全有可能在脚本中有一个拼写错误,它创建了创建VM的请求,但没有更有用的错误,我不确定它是什么。

如果有人对如何获得更有用的错误或更好的方法有任何建议,我会永远感激

python脚本的相关部分是:

def get_token(self):
    url = self._auth_url[:(self._auth_url.find('/'))]
    uri = self._auth_url[(self._auth_url.find('/')):]

    conn = httplib.HTTPSConnection( url, 443 )
    conn.request( 'POST', uri, body = "grant_type=client_credentials&client_id=%s&client_secret=%s&resource=https://management.azure.com/" % (self._app_id, self._app_secret), headers = { 'Content-Type' : 'application/x-www-form-urlencoded' } )
    resp = conn.getresponse()

    if not resp.status == 200:
        logger.error( resp.read() )
        raise Error( "Getting azure token failed with givein parameters. Check the _auth_url, _app_id, or _app_secret")
    self._connection = httplib.HTTPSConnection( "management.azure.com", 443 )
    return json.loads( resp.read() )[ "access_token" ]

def create_virtual_machine( self, resource_group_name, vm_name, os_type, role_size, disk_size, nic):
    body = self.generate_linux_virtual_machine_json( vm_name, role_size, disk_size, os_type, resource_group_name, nic )
    token = self.get_token()
    uri = "https://management.azure.com/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s&api-version=2016-08-30" % (self._subscription_id, resource_group_name, vm_name)

    self._connection.request( 'PUT', uri, body = body, headers = { 'Content-Type' : 'application/json', 'Authorization' : 'Bearer %s' % token } )
    resp = self._connection.getresponse()
    if not resp.status == 200:
        logger.error("status: %s, error: %s" % (resp.status, resp.read() ) )

def generate_linux_virtual_machine_json( self, vm_name, role_size, disk_size, os_type, resource_group_name, nic ):
    (publisher_name, publisher_offer, sku) = self.get_image_info( os_type )
    create_linux_virtual_machine_json = '''
    {
        "name": "%s",
        "location": "%s", 
        "properties":{
            "hardwareProfile": {
                "vmSize": "%s"
            },
            "storageProfile": {
                "imageReference": {
                    "publisher": "%s",
                    "offer": "%s",
                    "sku": "%s",
                    "version": "latest"
                },
                "osDisk": {
                    "name": "osdisk",
                    "osType": "Linux",
                    "createOption": "fromImage",
                    "diskSizeGB": "%s",
                    "caching": "ReadWrite"
                }
            },
            "osProfile": {
                "computerName": "%s",
                "adminUsername": "********",
                "adminPassword": "********",
                "customData": "",
                "linuxConfiguration": {
                    "disablePasswordAuthentication": false
                }
            },
            "networkProfile": {
                "networkInterfaces": [
                    {
                        "id": "%s",
                        "properties": {
                            "primary": true
                        } 
                    }
                ]
            }
        }
    }
    '''%(vm_name, self._location, role_size, publisher_name, publisher_offer, sku, disk_size, vm_name, nic)

    return create_linux_virtual_machine_json

1 个答案:

答案 0 :(得分:1)

我相信我弄明白了这个问题。在我使用的文档中,它将请求URI指定为:

&#34; ... /提供商/ Microsoft.Compute / virtualMachines / {VM}&安培; API-版本= {apiVersion}&#34;

但是,它应该是:

&#34; ... / providers / Microsoft.Compute / virtualMachines / {vm} api-version = {apiVersion}&#34;用问号而不是&符号

我已经在我的代码中更新了一切,似乎一切正常。