Code Monkey home page Code Monkey logo

fc-go-sdk's Introduction

Aliyun FunctionCompute Go SDK

API Reference :

FC API

GitHub Version Build Status

VERSION

go >= 1.8

Overview

Aliyun FunctionCompute Go SDK, sample

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/fc-go-sdk"
)

func main() {
	serviceName := "service555"
	client, _ := fc.NewClient(os.Getenv("ENDPOINT"), "2016-08-15", os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))

	fmt.Println("Creating service")
	createServiceOutput, err := client.CreateService(fc.NewCreateServiceInput().
		WithServiceName(serviceName).
		WithDescription("this is a smoke test for go sdk"))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
	if createServiceOutput != nil {
		fmt.Printf("CreateService response: %s \n", createServiceOutput)
	}

	// GetService
	fmt.Println("Getting service")
	getServiceOutput, err := client.GetService(fc.NewGetServiceInput(serviceName))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("GetService response: %s \n", getServiceOutput)
	}

	// UpdateService
	fmt.Println("Updating service")
	updateServiceInput := fc.NewUpdateServiceInput(serviceName).WithDescription("new description")
	updateServiceOutput, err := client.UpdateService(updateServiceInput)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("UpdateService response: %s \n", updateServiceOutput)
	}

	// UpdateService with IfMatch
	fmt.Println("Updating service with IfMatch")
	updateServiceInput2 := fc.NewUpdateServiceInput(serviceName).WithDescription("new description2").
		WithIfMatch(updateServiceOutput.Header.Get("ETag"))
	updateServiceOutput2, err := client.UpdateService(updateServiceInput2)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("UpdateService response: %s \n", updateServiceOutput2)
	}

	// UpdateService with wrong IfMatch
	fmt.Println("Updating service with wrong IfMatch")
	updateServiceInput3 := fc.NewUpdateServiceInput(serviceName).WithDescription("new description2").
		WithIfMatch("1234")
	updateServiceOutput3, err := client.UpdateService(updateServiceInput3)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("UpdateService response: %s \n", updateServiceOutput3)
	}

	// ListServices
	fmt.Println("Listing services")
	listServicesOutput, err := client.ListServices(fc.NewListServicesInput().WithLimit(100))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("ListServices response: %s \n", listServicesOutput)
	}

	// CreateFunction
	fmt.Println("Creating function1")
	createFunctionInput1 := fc.NewCreateFunctionInput(serviceName).WithFunctionName("testf1").
        		WithDescription("go sdk test function").
        		WithHandler("main.my_handler").WithRuntime("python2.7").
        		WithCode(fc.NewCode().WithFiles("./testCode/hello_world.zip")).
        		WithTimeout(5)

	createFunctionOutput, err := client.CreateFunction(createFunctionInput1)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("CreateFunction response: %s \n", createFunctionOutput)
	}
	fmt.Println("Creating function2")
	createFunctionOutput2, err := client.CreateFunction(createFunctionInput1.WithFunctionName("testf2"))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("CreateFunction response: %s \n", createFunctionOutput2)
	}

	// ListFunctions
	fmt.Println("Listing functions")
	listFunctionsOutput, err := client.ListFunctions(fc.NewListFunctionsInput(serviceName).WithPrefix("test"))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("ListFunctions response: %s \n", listFunctionsOutput)
	}

	// UpdateFunction
	fmt.Println("Updating function")
	updateFunctionOutput, err := client.UpdateFunction(fc.NewUpdateFunctionInput(serviceName, "testf1").
		WithDescription("newdesc"))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("UpdateFunction response: %s \n", updateFunctionOutput)
	}

	// InvokeFunction
	fmt.Println("Invoking function, log type Tail")
	invokeInput := fc.NewInvokeFunctionInput(serviceName, "testf1").WithLogType("Tail")
	invokeOutput, err := client.InvokeFunction(invokeInput)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("InvokeFunction response: %s \n", invokeOutput)
		logResult, err := invokeOutput.GetLogResult()
		if err != nil {
			fmt.Printf("Failed to get LogResult due to %v\n", err)
		} else {
			fmt.Printf("Invoke function LogResult %s \n", logResult)
		}
	}

	fmt.Println("Invoking function, log type None")
	invokeInput = fc.NewInvokeFunctionInput(serviceName, "testf1").WithLogType("None")
	invokeOutput, err = client.InvokeFunction(invokeInput)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("InvokeFunction response: %s \n", invokeOutput)
	}

    // PublishServiceVersion
	fmt.Println("Publishing service version")
	publishServiceVersionInput := fc.NewPublishServiceVersionInput(serviceName)
	publishServiceVersionOutput, err := client.PublishServiceVersion(publishServiceVersionInput)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("PublishServiceVersion response: %s \n", publishServiceVersionOutput)
	}

    // PublishServiceVersion with IfMatch
    fmt.Println("Publishing service version with IfMatch")
    publishServiceVersionInput2 := fc.NewPublishServiceVersionInput(serviceName).
	       WithIfMatch(getServiceOutput.Header.Get("ETag"))
	publishServiceVersionOutput2, err := client.PublishServiceVersion(publishServiceVersionInput2)
	if err != nil {
	     fmt.Fprintln(os.Stderr, err)
	} else {
	      fmt.Printf("PublishServiceVersion response: %s \n", publishServiceVersionOutput2)
	}

	// PublishServiceVersion with wrong IfMatch
	fmt.Println("Publishing service with wrong IfMatch")
	publishServiceVersionInput3 := fc.NewPublishServiceVersionInput(serviceName).
	       WithIfMatch("1234")
	publishServiceVersionOutput3, err := client.PublishServiceVersion(publishServiceVersionInput3)
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("PublishServiceVersion response: %s \n", publishServiceVersionOutput3)
	}

	// ListServiceVersions
	fmt.Println("Listing service versions")
	listServiceVersionsOutput, err := client.ListServiceVersions(fc.NewListServiceVersionsInput(serviceName).WithLimit(10))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("ListServiceVersions response: %s \n", listServiceVersionsOutput)
	}

	// GetService with qualifier
	fmt.Println("Getting service with qualifier")
	getServiceOutput2, err := client.GetService(fc.NewGetServiceInput(serviceName).WithQualifier(publishServiceVersionOutput.VersionID))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("GetService with qualifier response: %s \n", getServiceOutput2)
	}

	// CreateAlias
	aliasName := "alias"
	fmt.Println("Creating alias")
	createAliasOutput, err := client.CreateAlias(fc.NewCreateAliasInput(serviceName).WithAliasName(aliasName).WithVersionID(publishServiceVersionOutput.VersionID))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("CreateAlias response: %s \n", createAliasOutput)
	}

	// GetAlias
	fmt.Println("Getting alias")
	getAliasOutput, err := client.GetAlias(fc.NewGetAliasInput(serviceName, aliasName))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("GetAlias response: %s \n", getAliasOutput)
	}

	// UpdateAlias
	fmt.Println("Updating alias")
	updateAliasOutput, err := client.UpdateAlias(fc.NewUpdateAliasInput(serviceName, aliasName).WithVersionID(publishServiceVersionOutput2.VersionID))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("UpdateAlias response: %s \n", updateAliasOutput)
	}

	// ListAliases
	fmt.Println("Listing aliases")
	listAliasesOutput, err := client.ListAliases(fc.NewListAliasesInput(serviceName))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("ListAliases response: %s \n", listAliasesOutput)
	}

	// DeleteAlias
	fmt.Println("Deleting aliases")
	deleteAliasOutput, err := client.DeleteAlias(fc.NewDeleteAliasInput(serviceName, aliasName))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("DeleteAlias response: %s \n", deleteAliasOutput)
	}

	// DeleteServiceVersion
	fmt.Println("Deleting service version")
	deleteServiceVersionOutput, err := client.DeleteServiceVersion(fc.NewDeleteServiceVersionInput(serviceName, publishServiceVersionOutput.VersionID))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("DeleteServiceVersion response: %s \n", deleteServiceVersionOutput)
	}

	deleteServiceVersionOutput2, err := client.DeleteServiceVersion(fc.NewDeleteServiceVersionInput(serviceName, publishServiceVersionOutput2.VersionID))
	if err != nil {
	       fmt.Fprintln(os.Stderr, err)
	} else {
	       fmt.Printf("DeleteServiceVersion response: %s \n", deleteServiceVersionOutput2)
	}

	// DeleteFunction
	fmt.Println("Deleting functions")
	listFunctionsOutput, err = client.ListFunctions(fc.NewListFunctionsInput(serviceName).WithLimit(10))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("ListFunctions response: %s \n", listFunctionsOutput)
		for _, fuc := range listFunctionsOutput.Functions {
			fmt.Printf("Deleting function %s \n", *fuc.FunctionName)
			if output, err := client.DeleteFunction(fc.NewDeleteFunctionInput(serviceName, *fuc.FunctionName)); err != nil {
				fmt.Fprintln(os.Stderr, err)
			} else {
				fmt.Printf("DeleteFunction response: %s \n", output)
			}

		}
	}

	// DeleteService
	fmt.Println("Deleting service")
	deleteServiceOutput, err := client.DeleteService(fc.NewDeleteServiceInput(serviceName))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Printf("DeleteService response: %s \n", deleteServiceOutput)
	}

	// PutProvisionConfig
    fmt.Println("Putting provision config")
    putProvisionConfigOutput, err := client.PutProvisionConfig(fc.NewPutProvisionConfigInput(serviceName, "testAliasName", "testFunctionName").WithTarget(int64(100))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("PutProvisionConfig response: %s \n", putProvisionConfigOutput)
    }

    // GetProvisionConfig
    fmt.Println("Getting provision config")
    getProvisionConfigOutput, err := client.GetProvisionConfig(fc.NewGetProvisionConfigInput(serviceName, "testAliasName", "testFunctionName"))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("GetProvisionConfig response: %s \n", getProvisionConfigOutput)
    }

    // ListProvisionConfigs
    fmt.Println("Listing provision configs")
    listProvisionConfigsOutput, err := client.ListProvisionConfigs(fc.NewListProvisionConfigsInput())
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("ListProvisionConfigs response: %s \n", listProvisionConfigsOutput)
    }
}

More resources

Contacting us

License

fc-go-sdk's People

Contributors

alifyb avatar andymanastorm avatar arlenmbx avatar chandaoh avatar cici503 avatar fanzhe328 avatar hryang avatar ohyee avatar rocaltair avatar rockuw avatar rsonghuster avatar shuaichang avatar shuting-yst avatar squatstonight avatar tw108174 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fc-go-sdk's Issues

Process exited unexpectedly before completing request

请问在触发函数时产生 Process exited unexpectedly before completing request 的原因
InvokeFunction

	invokeInput := fc.NewInvokeFunctionInput("xxxx", "xxxx")
	invokeOutput, err := client.InvokeFunction(invokeInput)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	} else {
		fmt.Println(string(invokeOutput.Payload))
	}

return

{"errorMessage":"Process exited unexpectedly before completing request (duration: 9ms, maxMemoryUsage: 49.82MB)"}

readme 过分简单!

你好,开发者,麻烦加上
1、go get github.com/aliyun/fc-go-sdk
2、import "github.com/aliyun/fc-go-sdk"
买了阿里的产品,希望有一个完善的SDK,包括文档。

Create service got an error

I invoked CreateService method to create an fc service and got the following error:

&url.Error{Op:"Post", URL:"http://cn-beijing.fc.aliyuncs.com/2016-08-15/services", Err:(*net.OpError)(0xc42038c8c0)}

go mod 无法引用tag v1.1.0 和tag v1.0.0

因为我们使用的go版本是1.15 但是master分支已经升级到go 1.16了,其中部分代码使用了go1.16才有的io包,导致我们引用master分支的时候编译报错。但是在引用 tag v1.1.0 和tag v1.0.0的时候都报错:go: github.com/aliyun/[email protected]: reading github.com/aliyun/fc-go-sdk/go.mod at revision v1.1.0: unknown revision v1.1.0
能否将client.go: 1163行的io.ReadAll(resp.Body)改成ioutil.ReadAll(resp.Body), 这个文件里同时在使用io.ReadAll(resp.Body)和ioutil.ReadAll(resp.Body),而io.ReadAll(resp.Body)只在go1.16才支持,对老项目很不友好。

这么严重的问题为啥没人发现 。。。。

我使用sdk里的GetAuthStr方法调用http触发器。。。resource 参数,总是报错403 。。。后来看了python版的sdk发现。。。。。。。。。。结尾后要跟一个换行符( \n ) 才行。。。

(黑人问号)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.