@@ -15,6 +15,10 @@ import (
1515 "github.com/cloudfoundry/libbuildpack"
1616)
1717
18+ const (
19+ appDynamicsServiceNameRegex = "app(-)?dynamics"
20+ )
21+
1822type Command interface {
1923 Execute (string , io.Writer , io.Writer , string , ... string ) error
2024}
@@ -27,6 +31,7 @@ type AppdynamicsHook struct {
2731
2832type Plan struct {
2933 Credentials Credential `json:"credentials"`
34+ Name string `json:"name"`
3035}
3136
3237type Credential struct {
@@ -160,27 +165,14 @@ func (h AppdynamicsHook) BeforeCompile(stager *libbuildpack.Stager) error {
160165 return nil
161166 }
162167
163- var appdServiceName string
164- for serviceName := range services {
165- if match , err := regexp .MatchString ("app(-)?dynamics" , serviceName ); err != nil {
166- return nil
167- } else if match {
168- appdServiceName = serviceName
169- h .Log .Warning ("[DEPRECATION WARNING]:" )
170- h .Log .Warning ("Please use AppDynamics extension buildpack for Python Application instrumentation" )
171- h .Log .Warning ("for more details: https://docs.pivotal.io/partners/appdynamics/multibuildpack.html" )
172- break
173- }
174- }
168+ appdServiceName , appdynamicsPlan , err := getAppDynamicsServiceName (services , h .Log )
175169
176170 if appdServiceName == "" {
177171 return nil
178172 }
179173
180174 h .Log .BeginStep ("Setting up Appdynamics" )
181175
182- val := services [appdServiceName ]
183- appdynamicsPlan := val [0 ].Credentials
184176 vcapApplication := os .Getenv ("VCAP_APPLICATION" )
185177 application := VcapApplication {}
186178
@@ -191,18 +183,20 @@ func (h AppdynamicsHook) BeforeCompile(stager *libbuildpack.Stager) error {
191183
192184 sslFlag := "off"
193185
194- if appdynamicsPlan .SslEnabled {
186+ credentials := appdynamicsPlan .Credentials
187+
188+ if credentials .SslEnabled {
195189 sslFlag = "on"
196190 }
197191
198192 appdEnv := map [string ]string {
199193 "APPD_APP_NAME" : h .getEnv ("APPD_APP_NAME" , application .ApplicationName ),
200194 "APPD_TIER_NAME" : h .getEnv ("APPD_TIER_NAME" , application .ApplicationName ),
201195 "APPD_NODE_NAME" : h .getEnv ("APPD_NODE_NAME" , application .ApplicationName ),
202- "APPD_CONTROLLER_HOST" : appdynamicsPlan .ControllerHost ,
203- "APPD_CONTROLLER_PORT" : appdynamicsPlan .ControllerPort ,
204- "APPD_ACCOUNT_ACCESS_KEY" : appdynamicsPlan .AccountAccessKey ,
205- "APPD_ACCOUNT_NAME" : appdynamicsPlan .AccountName ,
196+ "APPD_CONTROLLER_HOST" : credentials .ControllerHost ,
197+ "APPD_CONTROLLER_PORT" : credentials .ControllerPort ,
198+ "APPD_ACCOUNT_ACCESS_KEY" : credentials .AccountAccessKey ,
199+ "APPD_ACCOUNT_NAME" : credentials .AccountName ,
206200 "APPD_SSL_ENABLED" : sslFlag ,
207201 }
208202
@@ -224,6 +218,49 @@ func (h AppdynamicsHook) BeforeCompile(stager *libbuildpack.Stager) error {
224218 return nil
225219}
226220
221+ func getAppDynamicsServiceName (services map [string ][]Plan , log * libbuildpack.Logger ) (string , Plan , error ) {
222+ // Check if there is a service with name appdynamics or app-dynamics
223+ for serviceName , servicePlans := range services {
224+ if isAppDynamicsServiceName (serviceName ) {
225+ appdServiceName := serviceName
226+ logDeprecationWarning (log )
227+ return appdServiceName , servicePlans [0 ], nil
228+ }
229+ }
230+
231+ // If this line is reached, no service with name appdynamics or app-dynamics was found. Check for user-provided services
232+ userProvidedServices , keyExists := services ["user-provided" ]
233+ if ! keyExists {
234+ return "" , Plan {}, nil
235+ }
236+
237+ for _ , plan := range userProvidedServices {
238+ if isAppDynamicsServiceName (plan .Name ) {
239+ appdServiceName := plan .Name
240+ logDeprecationWarning (log )
241+ return appdServiceName , plan , nil
242+ }
243+ }
244+
245+ // If this line is reached, no service with name appdynamics or app-dynamics was found in either the services and user-provided services. Return empty string, empty plan and nil error
246+ return "" , Plan {}, nil
247+ }
248+
249+ func isAppDynamicsServiceName (serviceName string ) bool {
250+ match , err := regexp .MatchString (appDynamicsServiceNameRegex , serviceName )
251+ if err != nil {
252+ return false
253+ }
254+
255+ return match
256+ }
257+
258+ func logDeprecationWarning (log * libbuildpack.Logger ) {
259+ log .Warning ("[DEPRECATION WARNING]:" )
260+ log .Warning ("Please use AppDynamics extension buildpack for Python Application instrumentation" )
261+ log .Warning ("for more details: https://docs.pivotal.io/partners/appdynamics/multibuildpack.html" )
262+ }
263+
227264func init () {
228265 logger := libbuildpack .NewLogger (os .Stdout )
229266 command := & libbuildpack.Command {}
0 commit comments