Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add api to route of openstack-wsgi

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to add api to the route of openstack-wsgi. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

What we are going to talk about today is that when we have generated such a url in the future, the next step is how the wsgi framework calls the corresponding code at the bottom of nova according to url.

Before I explain this, recall to check out routes's released api. For more details, please see the article: detailed explanation of openstack_ice 's wsgi (paste from ini configuration file-> routesr release process)

1. First print out the route mapper information:

Vim / usr/lib/python2.6/site-packages/nova/api/openstack/__init__.py

Restart the openstack-nova-api service

Service openstack-nova-api restart

Tailf / var/log/nova/api.log

The route information printed above is that the underlying layer adds api to the mapper of route through wsgi, and publishes it to the external call.

2. Next, we will explain how to add custom api, add it to route mapper and publish it to external calls.

Note: next, take the network module as an example, add a test_api interface.

The following figure shows the route information of network before it is added:

Through the analysis of the previous article, find the entrance to wsgi:

Vim / usr/lib/python2.6/site-packages/nova/api/openstack/compute/contrib/os_networks.py

Os_network is encapsulated by openstack and is used to add classes that map route mapper routing:

In

Add the following code: test_member:post, test_collection:

Each time the addition is completed, you need to republish and restart the openstack-nova-api service.

After restarting, view the os-network routing information as shown below:

As shown in the figure above, it is found that test_member and test_collection routing information has been released successfully.

The api released by member_actions is for each specific object, such as getting all the information of person with an id of 1. Therefore, you need to add the id field attribute before url.

The api released by collection_actions is for a collection of objects, such as getting information about all person.

For more information about route, please refer to the article: wsgi-restful-routes details

3. Explanation: when the api is released successfully, after the background route controller receives the matching url, how to control the call to the background code

There are roughly three ways to generate url:

1: assembled with dashboard via novaclient. (the network is assembled by neutronclient. For simplicity, subsequent articles only mention novaclient by default.)

2: generated by system commands such as nova list.

3: self-assembled through curl.

For the first and second ways, please refer to the previous article for details. Next, we will describe in detail how to develop a custom api if you call the released route through curl.

Note: the underlying api development is mostly carried out in the third way.

The first step is to obtain token:

Execute nova-- debug list:

The value after X-Auth-Token is the token to be obtained.

Step 2:

Add the token value to the environment variable (or do not add the environment variable, bring it with each request)

Vim vim .bashrc

This allows you to directly use $token to get the value of token later.

Export .bashrc file

Echo $token will see the token value

Step 3: send a url request:

Curl-I 'http://192.168.1.158:8774/v2/b077619bde964a4eb924db869fba977d/servers/f664204b-d4ec-4d01-aec9-757719a8d06b'-X GET-H "X-Auth-Project-Id: admin"-H "User-Agent: python-novaclient"-H "Accept: application/json"-H "X-Auth-Token:$token"

Corresponding route is:

When the url request is executed, the route is found correspondingly.

4. Request custom routing test_collection:

According to the routing information, the stitching url is:

Curl-I 'http://192.168.1.158:8774/v2/b077619bde964a4eb924db869fba977d/os-networks/test_collection'-X POST-H "X-Auth-Project-Id: admin"-H "User-Agent: python-novaclient"-H "Accept: application/json"-H "X-Auth-Token:$token"

Error 404 not found:

Open the route to publish the source code:

Vim / usr/lib/python2.6/site-packages/nova/api/openstack/compute/contrib/os_networks.py

Go to the routing controller: NetworkController ():

You can see that the routing controller class has methods show (), delete (), and so on.

Define the test_collection method in the controller class, as shown below:

The code added by restarting the nova-api service takes effect. (each time you modify the api module code, you must restart the service, which will not be repeated later.)

Request url again:

Curl-I 'http://192.168.1.158:8774/v2/b077619bde964a4eb924db869fba977d/os-networks/test_collection'-X POST-H "X-Auth-Project-Id: admin"-H "User-Agent: python-novaclient"-H "Accept: application/json"-H "X-Auth-Token:$token"

Api log:

In the test_collection function, you can add custom underlying code.

This is done by adding the underlying api.

After reading the above, do you have any further understanding of how to add api to openstack-wsgi 's route? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report