Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To Get A Single Instance Details #5906

Open
lbb-oussamabouchikhi opened this issue Mar 11, 2024 · 0 comments
Open

How To Get A Single Instance Details #5906

lbb-oussamabouchikhi opened this issue Mar 11, 2024 · 0 comments

Comments

@lbb-oussamabouchikhi
Copy link

How t get single instance details?

I could not find how to get a single instance using the api in the documentation. I am now fetching all instances and making a filter function to retrieve one instance, which is not optimal.

/**
     * @param {{
     *     instancePrefix: string;
     *     region: string;
     * }} param0
     * @returns {Promise<
     *     {
     *         name: string | undefined;
     *         id: string | undefined;
     *         status: string | undefined;
     *         ip: string | undefined;
     *     }[]
     * >}
     */
    async getInstancesFromProvider({ instancePrefix, region }) {
        try {
            const token = await getOSToken();

            // get current region
            const url_splitted = config.openstack.servers_url.split(".");
            url_splitted[1] = region.toLowerCase();

            const url = url_splitted.join(".");
            const headers = {
                "X-Auth-Token": token,
            };

            const response = await axios.get(url, { headers });

            // get instances array
            let instances = response.data.servers;

           // format return output
            instances = this.processInstancesFromOpenstack(instances);
            
            // retrive one instance using js filter function :/
            let instancesOS = instances.filter((instanceOS) => instanceOS.name.includes(instancePrefix));
            return instancesOS;
        } catch (error) {
            console.log(error);
            throw new CustomApiError("ovh.get-instances-error", {
                originalError: error,
            });
        }
    }

config

openstack: {
        token_url: "https://auth.cloud.ovh.net/v3/auth/tokens",
        token_payload: {
            auth: {
                identity: {
                    methods: ["password"],
                    password: {
                        user: {
                            name: "<name>",
                            domain: {
                                name: "Default",
                            },
                            password: "<password>",
                        },
                    },
                },
                scope: {
                    project: {
                        id: "project_id",
                    },
                },
            },
        },
        servers_url: "https://compute.de1.cloud.ovh.net/v2.1/<project_id>/servers/detail",
    },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant